Oftentimes, the exact structure of the XML is not important. What is more important to us is the data in it.
For example, in our users.xml, what we really need is the user's first and last name.
The exact structure of the XML is not important for us.
JAXB - Java API for XML Binding - allows us to directly get the data in the XML. We do not need to worry about the XML structure.
JAXB uses the XSD - XML Schema Descriptor - to generate classes which it can populate with the XML's data.
An XSD describes the structure of the XML. It is part of the formal specification of the XML. You expect to get it with the XML.
Let us use JAXB to parse users.xml.
First we need to have the XSD. In your /data folder, create a files users.xsd, like this:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="users">
<xs:complexType>
<xs:sequence>
<xs:element name="user" type="userType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="userType">
<xs:sequence>
<xs:element name="firstName" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="lastName" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
This XSD says that the XML will have a root element 'users', which can have any number of elements 'user', which will have two elements - firstName and lastName, each of type String.
Our users.xml confirms to this XSD.
Now, let us ask JAXB to generate the classes for this XSD.
JAXB is already shipped as part of your JDK. So we can run it from a command prompt.
Open a command prompt, cd to your /HelloWorld/data folder, and give it the following command:
C:\Documents and Settings\LavanniM\workspace-confucius\HelloWorld\data>xjc -p org.confucius.jaxb users.xsd -d "c:\Documents and Settings\LavanniM\workspace-confucius\HelloWorld\src"
parsing a schema...
compiling a schema...
org\confucius\jaxb\ObjectFactory.java
org\confucius\jaxb\UserType.java
org\confucius\jaxb\Users.java
xjc is the JAXB command line executable, -p tells JAXB which package the classes it will generate should belong to, -d tells it which directory to put the classes in.
Once we have generated these classes, we are ready to parse using JAXB.
In your /src/org/confucius folder, create a class TestJAXBParser.java, like this:
package org.confucius;
import java.io.File;
import java.util.Iterator;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import org.confucius.jaxb.UserType;
import org.confucius.jaxb.Users;
public class TestJAXBParser {
public static void main (String[] args){
try {
JAXBContext jc = JAXBContext.newInstance("org.confucius.jaxb");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Users users = (Users) unmarshaller.unmarshal(new File("data/users.xml"));
for (Iterator<UserType> iter = users.getUser().iterator(); iter.hasNext();){
UserType user = iter.next();
System.out.println("Got user: " + user.getFirstName() + " " + user.getLastName());
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
We create a JAXBContext, pointing it to the package where we have the JAXB classes, the create an 'unmarshaller' to parse the XML file.
A marshaller is an object which can convert an object to XML, an unmarshaller converts XML to objects.
R-click on the TestJAXBParser.java file in your Eclipse Navigator View, select Run As->Java Application.
You will see the users printed to the console.
Note that we extracted data from the XML - but our extract code has no knowledge of the XML (the XSD is not a part of the extract code, but a part of the XML formal specification). JAXB did whatever is necessary to give us the final objects.