JAXB 未按预期编组

发布于 2024-11-27 16:15:22 字数 1412 浏览 0 评论 0 原文

我的一个朋友让我发布这个问题:

编辑:他决定在这里自己发布这个问题: JAXB Unmarshalls XML In Correctly 我尝试删除此问题,但失败了。

我正在尝试编组一个名为 MyContainer 的 java 类。这个类扩展了我的另一个类,称为ContainerContainer 有一个名为: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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肥爪爪 2024-12-04 16:15:22

使用 @XmlElementWrapper 注释您的列表。请参阅 javadoc

Annotate your list with @XmlElementWrapper. See javadoc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文