JAXB:如何编组列表中的对象? [英] JAXB: How to marshal objects in lists?
问题描述
也许是一个愚蠢的问题:我有一个 类型的
List
,我想将它编组到 XML 文件中.这是我的类 Database
包含一个 ArrayList
...
Perhaps a stupid question: I have a List
of type <Data>
which I want to marshal into a XML file. This is my class Database
containing an ArrayList
...
@XmlRootElement
public class Database
{
List<Data> records = new ArrayList<Data>();
public List<Data> getRecords() { return records; }
public void setRecords(List<Data> records) { this.records = records; }
}
...这是类数据:
// @XmlRootElement
public class Data
{
String name;
String address;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
}
使用以下测试类...
public class Test
{
public static void main(String args[]) throws Exception
{
Data data1 = new Data();
data1.setName("Peter");
data1.setAddress("Cologne");
Data data2 = new Data();
data2.setName("Mary");
data2.setAddress("Hamburg");
Database database = new Database();
database.getRecords().add(data1);
database.getRecords().add(data2);
JAXBContext context = JAXBContext.newInstance(Database.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(database, new FileWriter("test.xml"));
}
}
...我得到了结果:
<database>
<records>
<address>Cologne</address>
<name>Peter</name>
</records>
<records>
<address>Hamburg</address>
<name>Mary</name>
</records>
</database>
但这不是我所期望的,即 对象的所有标记都丢失了.我正在寻找一种以以下结构导出数据的方法,但我不知道如何实现:
But that's not what I was expecting, i.e. all tags for <Data>
objects are missing. I am looking for a way to export the data in the following structure, but I don't know how to achieve this:
<database>
<records>
<data>
<address>Cologne</address>
<name>Peter</name>
</data>
<data>
<address>Hamburg</address>
<name>Mary</name>
</data>
</records>
</database>
<小时>
另外一个问题:如果我想在不使用@XmlElementWrapper
和@XmlElement
注解来处理问题,我可以引入一个中介班级
One additional question: if I want to deal with the problem without using @XmlElementWrapper
and @XmlElement
annotations, I can introduce an intermediary class
public class Records
{
List<Data> data = new ArrayList<Data>();
public List<Data> getData() { return data; }
public void setData(List<Data> data) { this.data = data; }
}
被修改后的基类使用
@XmlRootElement
public class Database
{
Records records = new Records();
public Records getRecords() { return records; }
public void setRecords(Records records) { this.records = records; }
}
在稍微修改的Test
类中:
...
Database database = new Database();
database.getRecords().getData().add(data1);
database.getRecords().getData().add(data2);
...
结果也是:
<database>
<records>
<data>
<address>Cologne</address>
<name>Peter</name>
</data>
<data>
<address>Hamburg</address>
<name>Mary</name>
</data>
</records>
</database>
这是根据上述 XML 文件结构创建 Java 类结构的推荐方式吗?
Is this the recommended way to create a Java class structure according to the XML file structure above?
推荐答案
在记录属性上添加:
@XmlElementWrapper(name="records")
@XmlElement(name="data")
有关 JAXB 和集合属性的更多信息,请参阅:
For more information on JAXB and collection properties see:
这篇关于JAXB:如何编组列表中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!