JAXB不像预期的那样马歇尔 [英] JAXB Does not Marshall as Expected

查看:64
本文介绍了JAXB不像预期的那样马歇尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个伙伴要我发这个问题:



编辑:他决定自己发布这个问题:JAXB Unmarshalls XML错误地我试图删除这个问题,但不能。



<我正在尝试编组一个名为 MyContainer 的java类。这个类扩展了我的另一个类,名为 Container Container 有一个名为的变量: protected List< ResourceReference< Component>>子



唯一的变量 MyContainer 具有从<$ c继承的变量$ C>容器。但是,当我将孩子列为 XmlList 时,此列表将不会与JAXB编组。



它将作为 XmlElement 进行编组,但这不是我想要显示数据的方式。当它被编组为 XmlElement 时,它会列出列表中的属性,但不会列出'heirarchially'它只列出这两个元素就好像它们是类的属性一样,而不是属于子列表。



如何让Jaxb编组列表(哪个实例化为ArrayList)正确?



编辑:这是一个例子:
这就是我得到的:

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

这更符合我的预期:

 < MyContainer name =Container Name> 
< booleanVal> false< / booleanVal>
< child> ///这将是扩展类的类变量,Container
< ResourceRef id =Entry 1>
< ResourceRef id =Entry 2>
< / child>
< /和>

当然,我目前的结果可能来自Container中的List,标有@XmlElement注释。但是,JAXB不会编组任何其他内容。

解决方案

使用 @XmlElementWrapper 。请参阅 javadoc


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.

I am trying to marshall a java class, called MyContainer. This class extends my another class, called Container. Container has a variable called: protected List<ResourceReference<Component>> child

The only variable MyContainer has is the one it inherits from Container. This List, however, will not marshall with JAXB when I have child listed as an XmlList.

It will marshall as an XmlElement, however this is not how I want the data displayed. 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.

How do I get Jaxb to marshall the List (Which is instantiated as an ArrayList) correctly?

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. However, JAXB will not marshall anything else..

解决方案

Annotate your list with @XmlElementWrapper. See javadoc.

这篇关于JAXB不像预期的那样马歇尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆