简体   繁体   English

JAXB不像预期的那样马歇尔

[英]JAXB Does not Marshall as Expected

A buddy of mine asked me to post this question: 我的一个朋友让我发这个问题:

EDIT: He decided to post the question on his own here: JAXB Unmarshalls XML Incorrectly I tried to delete this question but couldn't. 编辑:他决定在他自己发布这个问题: JAXB Unmarshalls XML错误地我试图删除这个问题,但不能。

I am trying to marshall a java class, called MyContainer . 我正在尝试编组一个名为MyContainer的java类。 This class extends my another class, called Container . 这个类扩展了另一个名为Container类。 Container has a variable called: protected List<ResourceReference<Component>> child Container有一个名为: protected List<ResourceReference<Component>> child的变量

The only variable MyContainer has is the one it inherits from Container . MyContainer唯一的变量是它从Container继承的变量。 This List , however, will not marshall with JAXB when I have child listed as an XmlList . 但是,当我将子列表作为XmlList时,此List将不会与JAXB编组。

It will marshall as an XmlElement , however this is not how I want the data displayed. 它将作为XmlElement编组,但这不是我想要显示数据的方式。 When it is marshalled as an XmlElement , it lists the attributes in the List , but not 'heirarchially' It just lists the two elements as if they were properties of the class, as opposed to belonging to the child list. 当它作为XmlElement编组时,它列出了List中的属性,但不是'heirarchially'它只列出了两个元素,就好像它们是类的属性一样,而不是属于子列表。

How do I get Jaxb to marshall the List (Which is instantiated as an ArrayList) correctly? 如何让Jaxb正确编组List (实例化为ArrayList)?

EDIT: Here's an example: This is what I am getting: 编辑:这是一个例子:这是我得到的:

<MyContainer name="Container Name">
    <booleanVal>false</booleanVal>
    <child id="Test Comp 1"/>
    <child id="Test Comp 2"/>
</and>

This is more what I expect: 这更符合我的期望:

<MyContainer name="Container Name">
    <booleanVal>false</booleanVal>
    <child> /// This would be the class variable of the extended Class, Container
        <ResourceRef id="Entry 1">
        <ResourceRef id="Entry 2">
    </child>
</and>

Admittly, my current results is probably from the List in Container being marked with the @XmlElement annotation. 我承认,我当前的结果可能来自于Container中的List,标有@XmlElement注释。 However, JAXB will not marshall anything else.. 但是,JAXB不会对其他任何东西进行编组。

Annotate your list with @XmlElementWrapper . 使用@XmlElementWrapper注释您的列表。 See javadoc . javadoc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM