我的一个朋友让我发布这个问题:
编辑:他决定在这里自己发布这个问题: JAXB Unmarshalls XML In Correctly 我尝试删除此问题,但失败了。
我正在尝试编组一个名为 MyContainer
的 java 类。这个类扩展了我的另一个类,称为Container
。 Container
有一个名为:protected List> 的变量。 child
MyContainer
拥有的唯一变量是它从 Container
继承的变量。但是,当我将子项列为 XmlList
时,此 List
不会与 JAXB 进行编组。
它将编组为 XmlElement
,但这不是我想要的数据显示方式。当它被编组为 XmlElement
时,它会列出 List
中的属性,但不是“heirarchially”。它只是列出两个元素,就好像它们是类的属性一样,而不是属于子列表。
如何让 Jaxb 正确编组 List
(实例化为 ArrayList)?
编辑:这是一个例子:
这就是我得到的:
<MyContainer name="Container Name">
<booleanVal>false</booleanVal>
<child id="Test Comp 1"/>
<child id="Test Comp 2"/>
</and>
这更多是我所期望的:
<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>
诚然,我当前的结果可能来自用 @XmlElement 注释标记的容器中的列表。但是,JAXB 不会整理任何其他内容。
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..
发布评论
评论(1)
使用
@XmlElementWrapper
注释您的列表。请参阅 javadoc。Annotate your list with
@XmlElementWrapper
. See javadoc.