Reputation: 299
I've got an issue with nested lists & was hoping someone could help.I would like to unmarshal the list in the below XML. I was hoping someone could point me in the right direction, I believe me annotations are off. When I try and unmarshall the MyList array it's returned null.
I have an 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 object:
@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>();
Any help would be much appreciated.
Upvotes: 3
Views: 868
Reputation:
The @XmlValue
are wrong. In addition, the @XmlElementWrapper
is wrong if the myList
elements are not wrapped.
Try this, it works for me.
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 + '}';
}
}
Main
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);
Output
Root{list=[MyList{name=N1, type=T1, version=V1}, MyList{name=N2, type=T2, version=V2}]}
Upvotes: 2
Reputation: 3994
try this, It should work.
@XmlRootElement(name = "root")
@XmlAccessorType (XmlAccessType.FIELD)
public class Root {
@XmlElement(name="myList")
private List<MyList> list = new ArrayList<MyList>();
}
unmarshal
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();
Upvotes: 1