忽略数组元素的父标签 XML 标签 JAXBContext

huangapple go评论101阅读模式
英文:

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 tag.

The xml file is like:


any name
..


123
transformers


567
joker

...

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

  1. }
  2. ...

]
}

英文:

I want to unmarshal a complex object using JAXBContext.

The object contains arrays, each element starts with <element> tag.

The xml file is like:

  1. &lt;root&gt;
  2. &lt;name&gt;any name&lt;/name&gt;
  3. ..
  4. &lt;movies&gt;
  5. &lt;element&gt;
  6. &lt;id&gt;123&lt;/id&gt;
  7. &lt;name&gt;transformers&lt;/name&gt;
  8. &lt;/element&gt;
  9. &lt;element&gt;
  10. &lt;id&gt;567&lt;/id&gt;
  11. &lt;name&gt;joker&lt;/name&gt;
  12. &lt;/element&gt;
  13. ...
  14. &lt;/movies&gt;
  15. &lt;/root&gt;

My pojo is :

  1. @XmlRootElement(name=&quot;root&quot;)
  2. public class Personalization {
  3. private String name;
  4. ..
  5. private Movie[] movies;
  6. }
  7. public class Movie{
  8. private String id;
  9. private String name;
  10. }

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 :

  1. {
  2. &quot;name&quot;:any name,
  3. ..
  4. &#39;movies&quot;: [
  5. {
  6. &quot;id&quot;: 123,
  7. &quot;name:&quot;transformers
  8. },
  9. {
  10. &quot;id&quot;:567,
  11. &quot;name&quot;:joker
  12. }
  13. ...
  14. ]
  15. }

答案1

得分: 1

你的电影列表上缺少一些注解,首先,你的列表是"wrapped":在"element"序列上有一个包含"movies"元素的容器,jaxb需要知道每个列表元素都被命名为"element",因此它会看起来像这样:

  1. @XmlRootElement(name="root")
  2. public class Personalization {
  3. private String name;
  4. ..
  5. @XmlElementWrapper(name="movies")
  6. @XmlElement(name ="element")
  7. private Movie[] movies;
  8. }
  9. public class Movie{
  10. private String id;
  11. private String name;
  12. }
英文:

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:

  1. @XmlRootElement(name=&quot;root&quot;)
  2. public class Personalization {
  3. private String name;
  4. ..
  5. @XmlElementWrapper(name=&quot;movies&quot;)
  6. @XmlElement(name =&quot;element&quot;)
  7. private Movie[] movies;
  8. }
  9. public class Movie{
  10. private String id;
  11. private String name;
  12. }

huangapple
  • 本文由 发表于 2020年8月11日 17:13:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63355126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定