如何将Java对象转换为XML- JAXB Marshalling

x33g5p2x  于2022-10-06 转载在 Java  
字(4.5k)|赞(0)|评价(0)|浏览(1312)

在这篇文章中,我们将学习如何使用Java Architecture for XML Binding (JAXB)将Java对象转换为XML。
Java Architecture for XML Binding (JAXB)是一个Java标准,它定义了Java对象如何从XML转换到XML。它使用一套标准的映射。

JAXB API提供了一个Marshaller接口,我们可以用marshal(write)方法将Java对象转换成XML文档。

让我们看看将Java对象转换为XML文档的步骤。

  1. 创建POJO或绑定模式并生成类
  2. 创建JAXBContext对象
  3. 创建Marshaller对象
  4. 通过使用集合方法创建内容树
  5. 调用marshal方法

###创建POJO类并添加JAXB注解

一些基本的和有用的JAXB注解是。

  1. @XmlRootElement。这是在JAXB中使用的Object必须有的注解。它定义了XML内容的根元素。
    1.@XmlType。它将类映射到XML模式类型。我们可以用它来排列XML中的元素。
  2. @XmlTransient。这将确保Object属性不被写入XML中。
  3. @XmlAttribute: 这将创建Object属性作为一个属性。
  4. @XmlElement(name = “ABC”): 这将创建名称为 "ABC "的元素。

Book POJO类

package net.javaguides.javaxmlparser.jaxb;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "book")
@XmlType(propOrder = {
    "author",
    "name",
    "publisher",
    "isbn"
})
public class Book {

    private String name;
    private String author;
    private String publisher;
    private String isbn;

    @XmlElement(name = "title")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }
}

书店POJO类

package net.javaguides.javaxmlparser.jaxb;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(namespace = "net.javaguides.javaxmlparser.jaxb")
public class Bookstore {

    @XmlElementWrapper(name = "bookList")
    @XmlElement(name = "book")
    private List < Book > bookList;
    private String name;
    private String location;

    public void setBookList(List < Book > bookList) {
        this.bookList = bookList;
    }

    public List < Book > getBooksList() {
        return bookList;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
}

Marshaller类--将Java对象转换为一个XML

package net.javaguides.javaxmlparser.jaxb;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
* Marshaller Class - Convert Java Object to XML
* 
* @author Ramesh Fadatare
*
*/
public class BookMain {
    private static final String BOOKSTORE_XML = "bookstore-jaxb.xml";

    public static void main(String[] args) throws JAXBException, IOException {

        List < Book > bookList = new ArrayList < Book > ();

        // create books
        Book book1 = new Book();
        book1.setIsbn("978-0134685991");
        book1.setName("Effective Java");
        book1.setAuthor("Joshua Bloch");
        book1.setPublisher("Amazon");
        bookList.add(book1);

        Book book2 = new Book();
        book2.setIsbn("978-0596009205");
        book2.setName("Head First Java, 2nd Edition");
        book2.setAuthor("Kathy Sierra");
        book2.setPublisher("amazon");
        bookList.add(book2);

        // create bookstore, assigning book
        Bookstore bookstore = new Bookstore();
        bookstore.setName("Amazon Bookstore");
        bookstore.setLocation("Newyorkt");
        bookstore.setBookList(bookList);

        convertObjectToXML(bookstore);

    }

    private static void convertObjectToXML(Bookstore bookstore) throws JAXBException, FileNotFoundException {
        // create JAXB context and instantiate marshaller
        JAXBContext context = JAXBContext.newInstance(Bookstore.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        // Write to System.out
        m.marshal(bookstore, System.out);

        // Write to File
        m.marshal(bookstore, new File(BOOKSTORE_XML));
    }
}

上述程序创建了一个名为bookstore-jaxb.xml的文件,并将Book对象存储到这个XML文件中。

<?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
<ns2:bookstore xmlns:ns2="net.javaguides.javaxmlparser.jaxb">
    <bookList>
        <book>
            <author>Joshua Bloch</author>
            <title>Effective Java</title>
            <publisher>Amazon</publisher>
            <isbn>978-0134685991</isbn>
        </book>
        <book>
            <author>Kathy Sierra</author>
            <title>Head First Java, 2nd Edition</title>
            <publisher>amazon</publisher>
            <isbn>978-0596009205</isbn>
        </book>
    </bookList>
    <location>Newyorkt</location>
    <name>Amazon Bookstore</name>
</ns2:bookstore>

相关文章

微信公众号

最新文章

更多