使用jaxb将xml转换为java对象(解组)

9wbgstp7  于 2022-12-25  发布在  Java
关注(0)|答案(1)|浏览(162)

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

<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时,我不确定它会是什么。
根据我的理解,我创建了以下POJO:

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

bvjxkvbb1#

    • 测试**

Tests类上,我们将添加一个@XmlRootElement注解。这样做将让您的JAXB实现知道,当文档以这个元素开始时,它应该示例化这个类。JAXB是按例外配置的。这意味着你只需要在你的Map不同于默认的地方添加注解。由于testData属性与默认Map不同,我们将使用@XmlElement注解。您可能会发现以下教程很有帮助: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;
    }

}
    • 测试数据**

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

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;
    }
}
    • 演示**

下面的示例说明了如何使用JAXBAPI读取(解编组)XML并填充域模型,然后将结果写回(编组)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);
    }

}

相关问题