使用jaxb(unmarshal)将xml转换为java对象 [英] convert xml to java object using jaxb (unmarshal)

查看:292
本文介绍了使用jaxb(unmarshal)将xml转换为java对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XML,我需要将其转换为java对象。

I have the following XML and I need to convert it into a java object.

<tests>
    <test-data> 
         <title>BookTitle</title> 
         <book>BookName</book> 
         <count>64018</count> 
         <test-data> 
            <title>Book title1</title> 
            <book>Book Name1</book> 
            <count>5</count> 
         </test-data> 
         <test-data> 
            <title>Book title2</title> 
            <book>Book Name3</book> 
            <count>5</count> 
         </test-data> 
         <test-data> 
            <title>Book title3</title> 
            <book>Book Name3</book> 
            <count>4</count> 
         </test-data> 
    </test-data>
</tests>

当我使用JAXB转换它时,我不确定我的pojo是什么。

I am not sure what will be my pojo when I use JAXB to convert it.

根据我的理解,我创建了以下POJO:

I created the following POJOs as per my understanding:

public class Tests {

    TestData testData;

    public TestData getTestData() {
        return testData;
    }

    public void setTestData(TestData testData) {
        this.testData = testData;
    }
}

public class TestData {
    String title;
    String book;
    String count;

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getBook() {
        return book;
    }
    public void setBook(String book) {
        this.book = book;
    }
    public String getCount() {
        return count;
    }
    public void setCount(String count) {
        this.count = count;
    }
}

请帮助我。
提前致谢。

Please help me. Thanks in advance.

推荐答案

测试

测试类中,我们将添加 @XmlRootElement 注释。执行此操作将使您的JAXB实现知道当文档以此元素开头时,它应该实例化此类。 JAXB是异常配置,这意味着您只需添加注释与默认值不同的注释。由于 testData 属性与默认映射不同,我们将使用 @XmlElement 注释。您可能会发现以下教程很有帮助: http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted

On the Tests class we will add an @XmlRootElement annotation. Doing this will let your JAXB implementation know that when a document starts with this element that it should instantiate this class. JAXB is configuration by exception, this means you only need to add annotations where your mapping differs from the default. Since the testData property differs from the default mapping we will use the @XmlElement annotation. You may find the following tutorial helpful: http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted

package forum11221136;

import javax.xml.bind.annotation.*;

@XmlRootElement
public class Tests {

    TestData testData;

    @XmlElement(name="test-data")
    public TestData getTestData() {
        return testData;
    }

    public void setTestData(TestData testData) {
        this.testData = testData;
    }

}

TestData

在这个类中,我使用 @XmlType 注释来指定元素的排序顺序我添加了一个似乎缺失的 testData 属性。我还使用 @XmlElement 注释,原因与测试类中的原因相同。

On this class I used the @XmlType annotation to specify the order in which the elements should be ordered in. I added a testData property that appeared to be missing. I also used an @XmlElement annotation for the same reason as in the Tests class.

package forum11221136;

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlType(propOrder={"title", "book", "count", "testData"})
public class TestData {
    String title;
    String book;
    String count;
    List<TestData> testData;

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getBook() {
        return book;
    }
    public void setBook(String book) {
        this.book = book;
    }
    public String getCount() {
        return count;
    }
    public void setCount(String count) {
        this.count = count;
    }
    @XmlElement(name="test-data")
    public List<TestData> getTestData() {
        return testData;
    }
    public void setTestData(List<TestData> testData) {
        this.testData = testData;
    }
}

演示

下面是一个示例,说明如何使用JAXB API读取(解组)XML并填充域模型,然后将结果写入(编组)回XML。

Below is an example of how to use the JAXB APIs to read (unmarshal) the XML and populate your domain model and then write (marshal) the result back to XML.

package forum11221136;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Tests.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum11221136/input.xml");
        Tests tests = (Tests) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(tests, System.out);
    }

}

这篇关于使用jaxb(unmarshal)将xml转换为java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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