在这篇文章中,我们将学习如何使用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文档的步骤。
JAXBContext
对象Marshaller
对象###创建POJO类并添加JAXB注解
一些基本的和有用的JAXB注解是。
@XmlRootElement
。这是在JAXB中使用的Object必须有的注解。它定义了XML内容的根元素。@XmlType
。它将类映射到XML模式类型。我们可以用它来排列XML中的元素。@XmlTransient
。这将确保Object属性不被写入XML中。@XmlAttribute
: 这将创建Object属性作为一个属性。@XmlElement(name = “ABC”)
: 这将创建名称为 "ABC "的元素。
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;
}
}
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;
}
}
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>
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.javaguides.net/2018/10/how-to-convert-java-object-to-xml-jaxb-marshalling.html
内容来源于网络,如有侵权,请联系作者删除!