JAXB:如何Marshal在列表中的对象? [英] JAXB: How to marshal objects in lists?

查看:167
本文介绍了JAXB:如何Marshal在列表中的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许一个愚蠢的问题:我有一个列表键入&LT的;数据> 我要元帅成一个XML文件。这是我的课数据库包含的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; }
}

...这是类数据:

...and this is class Data:

// @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; }
}

使用以下测试类...

Using the following test class...

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"));       
    }
}

...我得到的结果:

...I got the result:

<database>
    <records>
        <address>Cologne</address>
        <name>Peter</name>
    </records>
    <records>
        <address>Hamburg</address>
        <name>Mary</name>
    </records>
</database>

但是,这不是我所期待的,即对于&LT所有标签;数据&GT; 对象缺少。我正在寻找一种方式来导出数据在下面的结构,但我不知道如何实现这一点:

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; }
}

所使用的改性基类

used by the modified base class

@XmlRootElement
public class Database
{
    Records records = new Records();

    public Records getRecords()                { return records; }
    public void    setRecords(Records records) { this.records = records; }
}

在稍加修改测试类:

...
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 & Collection Properties

这篇关于JAXB:如何Marshal在列表中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆