Recently in a Java project that I’m currently working on it was necessary to read remote XML data. Since my Java skills aren’t too strong and that I’ve never used Java and XML together I hit the road to search for help. I quickly found some articles and tutorials about dealing with XML within Java. I was trying to understand some concepts from one of the articles when I glimpsed the article year and to my surprise: 1996. Wow! I wasn’t looking for an history class so I moved on.

I have found other older articles in the way and even have tried on some code until I was pointed to the right direction by some buddies. That’s a big point of confusion to beginners and inexperienced Java developers in my opinion. There is a ton of information about Java out there and we must take care not to get caught by some old and deprecated functionality. But since I got it right let’s stop complaining and go strait to the point!

We are going to use the Java Architecture for XML Binding (JAXB). What is so special about it? Well, it takes an XML Schema and automatically creates all necessary classes with setters and getters to allow easy manipulation of the XML data. It binds the XML file to our code in such a way that we don’t have to know much about XML itself. The JAXB is already included in the new Java 1.6!

First we need an example XML file so we can generate the XML Schema but if you prefer you can write it by yourself! For this example I’m using the books.xml file which contains a list of books (you can download all files at the end of this post). Now that we have the XML sample we need a tool to generate the schema for us. I recommend this site: XML-2-XSD. It’s quick and easy. Input the .xml file and catch the resulting .xsd.

The second step is to generate the class files from the .xsd file. We are going to use a JAXB tool called ‘xjc’. It’s also very easy, just type the following in your command prompt to get all files:

$ xjc -p myxml books.xsd

The above command will generate all necessary class files and put them into a package called ‘myxml’. Now you can take a look into the generated source code to have an idea of what is coming next.

Note: I’m assuming that you have the Java bin folder in your system path. Otherwise you should access the ‘xjc’ tool from there.

The final step is the code that will take any XML file and bind it to our application. The act of getting the XML data is called ‘unmarshaling’. In the example I’m getting data from a local file but you can easily get it from a remote source passing an URL to the unmarshal method instead.

// starts the binding process
JAXBContext jc = JAXBContext.newInstance("myxml");
Unmarshaller u = jc.createUnmarshaller();
 
// unmarshal the xml file
Books books = (Books) u.unmarshal(
    getClass().getResource("books.xml")
);

Did you get it? See how now we have a Books type? It is the root node from where we get everything else. We also have a type for each XML node. This is due to the code generated by JAXB. Let’s keep moving!

// get the list of books
List<Books.Book> bookList = books.getBook();
 
// iterate through the list to get each book
for(int i=0; i < bookList.size(); i++){
 
    // get book
    Books.Book book = bookList.get(i);

From the code above you can see that we use the getter method getBook() to retrieve the List of books. Then we iterate through it to get each book node. We store each node in the book variable which is a Books.Book type. Directly from the Books.Book type we have access to all book nodes as follows:

    // get data
    String description  = book.getDescription();
    String format       = book.getFormat();
    String isbn         = book.getISBN();
    String itemId       = book.getItemId();
    String language     = book.getLanguage();
    String name         = book.getName();
    String pages        = book.getPages();
    String publication  = book.getPublicationDate();
    String publisher    = book.getPublisher();

Pretty easy! If an XML tag have other child tags we need to loop through it to get each node like we did with the root node. And again we use a loop to get the authors.

    // get the list of authors
    List<Books.Book.Authors> authorList = book.getAuthors();
 
    // iterate through the list to get each author
    for(int y=0; y < authorList.size(); y++){
 
        // get author
        Books.Book.Authors author = authorList.get(y);
 
        // get data
        String authorName = author.getAuthorName();
    }
}

And that’s all. A lot of talking for a small amount of code! In this example I have just stored the XML data into variables but you should be able to do much more. The key point is to understand the types generated by JAXB and use the getters to retrieve the information. Here I used lists to get all nodes but if you already know which node you need to access you can get it directly like this:

Books.Book.Authors author = 
    book.getAuthors().get(0).getAuthorName();

If you have any doubt feel free to leave a message. I’ll be glad to help whenever I can.

Files:
EasyXML Code
books.xml
books.xsd