Reputation: 127
I want to unmarshal a complex object using JAXBContext.
The object contains arrays, each element starts with tag.
The xml file is like:
<root>
<name>any name</name>
..
<movies>
<element>
<id>123</id>
<name>transformers</name>
</element>
<element>
<id>567</id>
<name>joker</name>
</element>
...
</movies>
</root>
My pojo is :
@XmlRootElement(name="root")
public class Personalization {
private String name;
..
private Movie[] movies;
}
public class Movie{
private String id;
private String name;
}
When i tried to do the mapping, the movies array contains null. When i removed the tags it worked. I have to keep the xml as it is because it is require to be in that format. How to ignore the tag in each movie element?
Note: I can't create Element class & embed the movie attributes there, because i need to map the same pojo to json formatted file below :
{
"name":any name,
..
'movies": [
{
"id": 123,
"name:"transformers
},
{
"id":567,
"name":joker
}
...
]
}
Upvotes: 0
Views: 675
Reputation: 190
You are missing some annotations on the movie list, first of all your list is "wrapped": you have a containing "movies" element on the sequence of "element", and jaxb has to know that each list element is named "element", so it would look like:
@XmlRootElement(name="root")
public class Personalization {
private String name;
..
@XmlElementWrapper(name="movies")
@XmlElement(name ="element")
private Movie[] movies;
}
public class Movie{
private String id;
private String name;
}
Upvotes: 1