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

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

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

}
...

]
}

英文:

I want to unmarshal a complex object using JAXBContext.

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

The xml file is like:

&lt;root&gt;
  &lt;name&gt;any name&lt;/name&gt;
  ..
  &lt;movies&gt;
    &lt;element&gt;
        &lt;id&gt;123&lt;/id&gt;
        &lt;name&gt;transformers&lt;/name&gt;
    &lt;/element&gt;
    &lt;element&gt;
        &lt;id&gt;567&lt;/id&gt;
        &lt;name&gt;joker&lt;/name&gt;
    &lt;/element&gt;
    ...
  &lt;/movies&gt;
&lt;/root&gt;

My pojo is :

@XmlRootElement(name=&quot;root&quot;)
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 :

{
  &quot;name&quot;:any name,
  ..
  &#39;movies&quot;: [
        {
        &quot;id&quot;: 123,
        &quot;name:&quot;transformers
        },
        {
        &quot;id&quot;:567,
        &quot;name&quot;: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=&quot;root&quot;)
public class Personalization {
 private String name;
 ..
 @XmlElementWrapper(name=&quot;movies&quot;)
 @XmlElement(name =&quot;element&quot;)
 private Movie[] movies;
}

public class Movie{
 private String id;
 private String name;
}

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:

确定