英文:
ignore parent tag of array elements xml tag JAXBContext
问题
I want to unmarshal a complex object using JAXBContext.
The object contains arrays, each element starts with
The xml file is like:
..
...
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
I have to keep the xml as it is because it is require to be in that format.
How to ignore the
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
}
...
]
}
英文:
I want to unmarshal a complex object using JAXBContext.
The object contains arrays, each element starts with <element> 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 <element> 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 <element> 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
}
...
]
}
答案1
得分: 1
你的电影列表上缺少一些注解,首先,你的列表是"wrapped":在"element"序列上有一个包含"movies"元素的容器,jaxb需要知道每个列表元素都被命名为"element",因此它会看起来像这样:
@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;
}
英文:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论