Java Easy Way to Turn Xml Into Pojo

How to Convert XML to Java Object - JAXB Unmarshalling

In this article, we will learn how to convert XML to Java object using Java Architecture for XML Binding (JAXB).

Java Architecture for XML Binding (JAXB) is a Java standard that defines how Java objects are converted from and to XML. It uses a standard set of mappings.

In JAXB term Converting XML to Java Object is called JAXB Unmarshalling. JAXB API provides the UnMarshaller interface, we can unmarshal(read) the object into an XML document.

Check out How to Convert Java Object to XML - JAXB Marshalling

Let's see the steps to convert XML document into Java object.

  • Create POJO or bind the schema and generate the classes
  • Create the JAXBContext object
  • Create the Unmarshaller objects
  • Call the unmarshal method
  • Use getter methods of POJO to access the data

Unmarshaller example: Converting XML document into Java object

XML Document

Here is XML file that we are going to convert to Java Object:

<?xml                              version=                "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>

Create POJO classes and Add JAXB annotations

Some basic and useful JAXB annotations are:

  1. @XmlRootElement: This is a must-have an annotation for the Object to be used in JAXB. It defines the root element for the XML content.
  2. @XmlType: It maps the class to the XML schema type. We can use it for ordering the elements in the XML.
  3. @XmlTransient: This will make sure that the Object property is not written to the XML.
  4. @XmlAttribute: This will create the Object property as an attribute.
  5. @XmlElement(name = "ABC"): This will create the element with name "ABC"

Book POJO Class

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

Bootstore POJO Class

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

Unmarshaller Class - Convert XML to Object in Java

              package              net.javaguides.javaxmlparser.jaxb;              import              java.io.File;              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.Unmarshaller;              /**                              * Unmarshaller Class - Convert XML to Object in Java                              *                @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);          convertXMLToObject();     }              private              static              Bookstore              convertXMLToObject() {              try              {              JAXBContext              context              =              JAXBContext              .newInstance(Bookstore              .class);              Unmarshaller              un              =              context.createUnmarshaller();              Bookstore              bookstore              =              (Bookstore) un.unmarshal(new              File(BOOKSTORE_XML));              List              <              Book              >              list              =              bookstore.getBooksList();              for              (Book              book:              list) {              System              .out.println(                "Book:                "                            +              book.getName()              +                              "                from                "                            +              book.getAuthor());             }         }              catch              (JAXBException              e) {             e.printStackTrace();         }              return              null;     } }

Output:

            Book: Effective Java from Joshua Bloch Book: Head First Java, 2nd Edition from Kathy Sierra          

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

sawyersatho1977.blogspot.com

Source: https://www.javaguides.net/2018/10/how-to-convert-xml-to-java-object-jaxb-unmarshalling.html

0 Response to "Java Easy Way to Turn Xml Into Pojo"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel