我遇到嵌套列表问题&我希望有人可以提供帮助。我想在下面的XML中解组列表。我希望有人可以指出我正确的方向,我相信我的注释是关闭的。当我尝试解组MyList数组时,它返回null。
我有一个XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<myList>
<name>N1</name>
<type>T1</type>
<version>V1</version>
</myList>
<myList>
<name>N2</name>
<type>T2</type>
<version>V2</version>
</myList>
</root>
MyList对象:
@XmlRootElement(name = "myList")
@XmlAccessorType(XmlAccessType.FIELD)
public class MyList {
@XmlValue
private String name;
@XmlValue
private String type;
@XmlValue
private String version;
Root Class:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Root {
@XmlElementWrapper(name="myLists")
@XmlElement(name="myList")
private List<MyList> list = new ArrayList<MyList>();
非常感谢任何帮助。
答案 0 :(得分:2)
@XmlValue
错了。此外,如果@XmlElementWrapper
元素未被包装,则myList
错误。
试试这个,它对我有用。
<强> Root.java
强>
package de.lhorn.so;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "root")
public class Root {
@XmlElement(name = "myList")
private List<MyList> list = new ArrayList<>();
public Root() {
}
public List<MyList> getList() {
return list;
}
public void setList(List<MyList> list) {
this.list = list;
}
@Override
public String toString() {
return "Root{" + "list=" + list + '}';
}
}
<强> MyList.java
强>
package de.lhorn.so;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "myList")
@XmlAccessorType(XmlAccessType.FIELD)
public class MyList {
private String name;
private String type;
private String version;
public MyList() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
@Override
public String toString() {
return "MyList{" + "name=" + name + ", type=" + type + ", version=" + version + '}';
}
}
主要强>
InputStream is = SOPlayground.class.getResourceAsStream("root.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Root root = (Root) jaxbUnmarshaller.unmarshal(is);
System.out.println(root);
<强>输出强>
Root{list=[MyList{name=N1, type=T1, version=V1}, MyList{name=N2, type=T2, version=V2}]}
答案 1 :(得分:1)
试试这个,它应该有用。
@XmlRootElement(name = "root")
@XmlAccessorType (XmlAccessType.FIELD)
public class Root {
@XmlElement(name="myList")
private List<MyList> list = new ArrayList<MyList>();
}
<强>解组强>
JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
//We had written this file in marshalling example
Root root= (Root) jaxbUnmarshaller.unmarshal( new File("c:/foo.xml") );
List<MyList> myLists = root.getList();