【问题标题】:JAXB adding unwanted tags in XML and removing desired onesJAXB 在 XML 中添加不需要的标签并删除所需的标签
【发布时间】:2013-09-02 11:05:13
【问题描述】:

我的班级结构如下:

@XmlRootElement(name = "storage")
@XmlType(propOrder = {
        "entries"
})
@XmlAccessorType(XmlAccessType.FIELD)
public class A {
    @XmlElement(name = "configuration")
    private Set<ClassB> entries;
    ...
}

@XmlRootElement(name = "configuration")
@XmlType(propOrder = {
        "name",
        "entries"
})
@XmlAccessorType(XmlAccessType.FIELD)
public class B {
    private String name;
    @XmlElement(name = "configurationEntry")
    private Set<ClassC> entries;
    ...
}

@XmlRootElement(name = "configurationEntry")
@XmlType(propOrder = {
        "property1",
        "property2",
        "property3"
})
@XmlAccessorType(XmlAccessType.FIELD)
public class C {
    private String property1;
    private String property2;
    private String property3;
    ...
}

当我使用 Jersey 制作一个 XML 时,当我尝试访问根容器(A 类)时得到以下输出。我不知道为什么有标签而不是标签。

<Bes>
    <configuration>
        <name>Name1</name>
        <configurationEntry>
            <property1>...</property1>
            <property2>...</property2>
            <property3>...</property2>
        </configurationEntry>
        <configurationEntry>
            <property1>...</property1>
            <property2>...</property2>
            <property3>...</property2>
        </configurationEntry>
    </configuration>
    <configuration>
        <name>Name2</name>
        <configurationEntry>
            <property1>...</property1>
            <property2>...</property2>
            <property3>...</property2>
        </configurationEntry>
        <configurationEntry>
            <property1>...</property1>
            <property2>..</property2>
            <property3>...</property2>
        </configurationEntry>
    </configuration>
</Bes>

当我尝试访问 ClassB 容器之一时,我得到以下信息。 (与之前类似的问题),而不是我得到并且没有包含标签。

<Aes>
    <configurationEntry>
        <property1>...</property1>
        <property2>...</property2>
        <property3>...</property2>
    </configurationEntry>
    <configurationEntry>
        <property1>...</property1>
        <property2>...</property2>
        <property3>...</property2>
    </configurationEntry>
<Aes>

唯一能按预期工作的是最低级别的 C 类

<configurationEntry>
    <property1>...</property1>
    <property2>...</property2>
    <property3>...</property2>
</configurationEntry>

为了清楚起见,我想要的输出如下:

访问存储(A 类)容器时:

<storage>
    <configuration>
        <name>Name1</name>
        <entries>
            <configurationEntry>
                <property1>...</property1>
                <property2>...</property2>
                <property3>...</property2>
            </configurationEntry>
            <configurationEntry>
                <property1>...</property1>
                <property2>...</property2>
                <property3>...</property2>
            </configurationEntry>
        </entries>
    </configuration>
    <configuration>
        <name>Name2</name>
        <entries>
            <configurationEntry>
                <property1>...</property1>
                <property2>...</property2>
                <property3>...</property2>
            </configurationEntry>
            <configurationEntry>
                <property1>...</property1>
                <property2>..</property2>
                <property3>...</property2>
            </configurationEntry>
        </entries>
    </configuration>
</storage>

访问二级容器 B 类时,我想要以下内容:

<configuration>
    <name>Name1</name>
    <entries>
        <configurationEntry>
            <property1>...</property1>
            <property2>...</property2>
            <property3>...</property2>
        </configurationEntry>
        <configurationEntry>
            <property1>...</property1>
            <property2>...</property2>
            <property3>...</property2>
        </configurationEntry>
    </entries>
</configuration>

C 类是可以的:

<configurationEntry>
    <property1>...</property1>
    <property2>...</property2>
    <property3>...</property2>
</configurationEntry>

【问题讨论】:

    标签: java xml jaxb jersey marshalling


    【解决方案1】:

    因为我在 A 类和 B 类中看到标签 &lt;Bes&gt;&lt;Aes&gt;.. 我觉得你已经定义了根元素.. 我的意思是 A 类和 B 类是另一个类的子集.. 检查这个是因为一个 xml 只能有一个根元素。如果是这样,那么类 A 和 B 中的根元素是无效的。我没有看到类中定义的 xml 有任何问题。欲了解更多信息,请访问http://java.dzone.com/articles/jaxb-and-root-elements

    【讨论】:

      【解决方案2】:

      没关系,我是个白痴。 -.-'

      在我的客户端代码中,出于某种原因,我分别检索了 Set&lt;B&gt;Set&lt;C&gt; 而不是 AB

      【讨论】:

        猜你喜欢
        • 2019-10-03
        • 1970-01-01
        • 2017-12-14
        • 2012-02-20
        • 2019-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多