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

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

在这篇文章中,我们将学习如何使用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类

  1. package net.javaguides.javaxmlparser.jaxb;
  2. import javax.xml.bind.annotation.XmlElement;
  3. import javax.xml.bind.annotation.XmlRootElement;
  4. import javax.xml.bind.annotation.XmlType;
  5. @XmlRootElement(name = "book")
  6. @XmlType(propOrder = {
  7. "author",
  8. "name",
  9. "publisher",
  10. "isbn"
  11. })
  12. public class Book {
  13. private String name;
  14. private String author;
  15. private String publisher;
  16. private String isbn;
  17. @XmlElement(name = "title")
  18. public String getName() {
  19. return name;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. public String getAuthor() {
  25. return author;
  26. }
  27. public void setAuthor(String author) {
  28. this.author = author;
  29. }
  30. public String getPublisher() {
  31. return publisher;
  32. }
  33. public void setPublisher(String publisher) {
  34. this.publisher = publisher;
  35. }
  36. public String getIsbn() {
  37. return isbn;
  38. }
  39. public void setIsbn(String isbn) {
  40. this.isbn = isbn;
  41. }
  42. }

书店POJO类

  1. package net.javaguides.javaxmlparser.jaxb;
  2. import java.util.List;
  3. import javax.xml.bind.annotation.XmlElement;
  4. import javax.xml.bind.annotation.XmlElementWrapper;
  5. import javax.xml.bind.annotation.XmlRootElement;
  6. @XmlRootElement(namespace = "net.javaguides.javaxmlparser.jaxb")
  7. public class Bookstore {
  8. @XmlElementWrapper(name = "bookList")
  9. @XmlElement(name = "book")
  10. private List < Book > bookList;
  11. private String name;
  12. private String location;
  13. public void setBookList(List < Book > bookList) {
  14. this.bookList = bookList;
  15. }
  16. public List < Book > getBooksList() {
  17. return bookList;
  18. }
  19. public String getName() {
  20. return name;
  21. }
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25. public String getLocation() {
  26. return location;
  27. }
  28. public void setLocation(String location) {
  29. this.location = location;
  30. }
  31. }

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

  1. package net.javaguides.javaxmlparser.jaxb;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import javax.xml.bind.JAXBContext;
  8. import javax.xml.bind.JAXBException;
  9. import javax.xml.bind.Marshaller;
  10. /**
  11. * Marshaller Class - Convert Java Object to XML
  12. *
  13. * @author Ramesh Fadatare
  14. *
  15. */
  16. public class BookMain {
  17. private static final String BOOKSTORE_XML = "bookstore-jaxb.xml";
  18. public static void main(String[] args) throws JAXBException, IOException {
  19. List < Book > bookList = new ArrayList < Book > ();
  20. // create books
  21. Book book1 = new Book();
  22. book1.setIsbn("978-0134685991");
  23. book1.setName("Effective Java");
  24. book1.setAuthor("Joshua Bloch");
  25. book1.setPublisher("Amazon");
  26. bookList.add(book1);
  27. Book book2 = new Book();
  28. book2.setIsbn("978-0596009205");
  29. book2.setName("Head First Java, 2nd Edition");
  30. book2.setAuthor("Kathy Sierra");
  31. book2.setPublisher("amazon");
  32. bookList.add(book2);
  33. // create bookstore, assigning book
  34. Bookstore bookstore = new Bookstore();
  35. bookstore.setName("Amazon Bookstore");
  36. bookstore.setLocation("Newyorkt");
  37. bookstore.setBookList(bookList);
  38. convertObjectToXML(bookstore);
  39. }
  40. private static void convertObjectToXML(Bookstore bookstore) throws JAXBException, FileNotFoundException {
  41. // create JAXB context and instantiate marshaller
  42. JAXBContext context = JAXBContext.newInstance(Bookstore.class);
  43. Marshaller m = context.createMarshaller();
  44. m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  45. // Write to System.out
  46. m.marshal(bookstore, System.out);
  47. // Write to File
  48. m.marshal(bookstore, new File(BOOKSTORE_XML));
  49. }
  50. }

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

  1. <?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
  2. <ns2:bookstore xmlns:ns2="net.javaguides.javaxmlparser.jaxb">
  3. <bookList>
  4. <book>
  5. <author>Joshua Bloch</author>
  6. <title>Effective Java</title>
  7. <publisher>Amazon</publisher>
  8. <isbn>978-0134685991</isbn>
  9. </book>
  10. <book>
  11. <author>Kathy Sierra</author>
  12. <title>Head First Java, 2nd Edition</title>
  13. <publisher>amazon</publisher>
  14. <isbn>978-0596009205</isbn>
  15. </book>
  16. </bookList>
  17. <location>Newyorkt</location>
  18. <name>Amazon Bookstore</name>
  19. </ns2:bookstore>

相关文章

最新文章

更多