What is JAXB?
JAXB stands for Java Architecture for XML Binding.
JAXB provides API (Application Programming Interface) for
converting Object to XML and XML to object quickly. JAXB is part of JDK 6.
Earlier JAXB was a separate project but later on it was included in JDK 6.
The XML and Java language are mostly used technology
partners to exchange information over the internet.
As we know now that, JAXB is used to convert XML to object
and vice versa, below are the terms described for the above phenomena:
1. Converting XML to Java Object: JAXB Unmarshalling.
2. Converting a Java object XML: JAXB Marshalling.
We will discuss JAXB unmarshalling with example
Lets start unmarshalling concept.
JAXB Unmarshalling.
Suppose we have below Books.xml to unmarshall.
Books.xml
--------------------------------------------
<?xml
version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella,
Matthew</author>
<title>XML Developer's
Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at
creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect
battles corporate zombies,
an evil sorceress, and her own childhood
to become queen
of the world.</description>
</book>
</catalog>
If you see
the XML, we have catalog tag as a root tag in XML.
So we will create
a class with that name as and create corresponding fields to XML tags available
in XML.
Catalog.java
--------------------------------------------------------
package com.launcher;
import
javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "catalog")
public class Catalog
{
private Book[] book;
public Book[] getBook ()
{
return book;
}
public void setBook (Book[] book)
{
this.book = book;
}
private String id;
private String genre;
private String author;
private String title;
private String price;
private String publish_date;
private String description;
public String getId ()
{
return id;
}
public void setId (String id)
{
this.id = id;
}
public String getGenre ()
{
return genre;
}
public void setGenre (String genre)
{
this.genre = genre;
}
public String getAuthor ()
{
return author;
}
public void setAuthor (String author)
{
this.author = author;
}
public String getTitle ()
{
return title;
}
public void setTitle (String title)
{
this.title = title;
}
public String getPrice ()
{
return price;
}
public void setPrice (String price)
{
this.price = price;
}
public String
getPublish_date ()
{
return publish_date;
}
public void setPublish_date
(String publish_date)
{
this.publish_date = publish_date;
}
public String
getDescription ()
{
return description;
}
public void setDescription
(String description)
{
this.description = description;
}
}
If you have
big XML file, you can use online POJO creator.
So to
perform unmarshalling, JAXBContext is the abstract class available in JAXB APIs to get the instance
of the class. JAXBContext provides the entery point to its API for client.
We have to call createUnmarshaller method of
the context and then unmarshall the XML file, as given below :
Client.java
-----------------------------------------------
import java.io.File;
import
javax.xml.bind.JAXBContext;
import
javax.xml.bind.JAXBException;
import
javax.xml.bind.Unmarshaller;
public class JaxbDemo {
public static void main(String[] args) throws JAXBException {
File
file = new File("books.xml");
JAXBContext
jaxbContext = JAXBContext.newInstance(Catalog.class);
Unmarshaller
jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Catalog
museums = (Catalog) jaxbUnmarshaller.unmarshal(file);
System.out.println(museums);
Book[]
book = museums.getBook();
for (int i = 0; i < book.length; i++) {
//this will print
all the aithor names of the corresponding books
System.out.println(++i+book[i].getAuthor());
}
}
}
Output:
------------------------------------
Gambardella, Matthew
No comments:
Post a Comment