gpt4 book ai didi

java - JAXB 在 XML 中添加不需要的标签并删除所需的标签

转载 作者:行者123 更新时间:2023-12-01 14:10:13 24 4
gpt4 key购买 nike

我的类(class)结构如下:

@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>

最佳答案

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

关于java - JAXB 在 XML 中添加不需要的标签并删除所需的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18571711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com