gpt4 book ai didi

java - 在jaxb中生成list标签和object标签

转载 作者:行者123 更新时间:2023-11-29 03:34:46 25 4
gpt4 key购买 nike

我想在jaxb中生成list标签和object标签。我有两个类(class)。

@XmlRootElement(name = "firstclass")
class Firstclass{
List<secondclass> secondclassList = new ArrayList<secondclass>();

//constructors, getters, setters

@XmlElement(name = "secondclassList")
public void setsecondclassList(List<Secondclass> secondclassList) {
this.secondclassList= secondclassList;
}

}

二级代码

@XmlRootElement(name = "secondclass")
class Secondclass{
String name;
String attribute;

//constructors, getters, setters
}

xml 转换后,我得到类似的输出

<firstclass>
<secondclassList>
<name>my name</name>
<attribute>attr value 11</attribute>
<secondclassList>
</secondclassList>
<name>my name</name>
<attribute>attr value 22</attribute>
</secondclassList>
</firstclass>

但我想要这样的输出

<firstclass>
<secondclassList>
<secondclass>
<name>my name</name>
<attribute>attr value 11</attribute>
</secondclass>
<secondclass>
<name>my name</name>
<attribute>attr value 22</attribute>
</secondclass>
</secondclassList>
</firstclass>

最佳答案

尝试使用:

  @XmlElementWrapper(name = "secondclassList")
@XmlElement(name="secondclass")
public void setsecondclassList(List<Secondclass> secondclassList) {

关于java - 在jaxb中生成list标签和object标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16167238/

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