main banner


JAXB, the Java tool to parse XML to Objects (Part 1)

Xml is used in a vast number of applications because it is flexible and easy to use as a form of communication between dissimilar systems.

It is a common scenario when a Java application receives information in the form of an xml, from another application or system (which can be any language, not just java); this xml can be parsed in different ways, the most basic as a String, looking for the xml elements with substrings, or considering each xml branch as a node, using XPath, or you could even use regular expressions to find concrete parts of the xml, all these approaches completely valid.

This article will be about parsing xml to java objects, or POJOs, and back to xml.

The main advantage of Jaxb (Java Architecture for Xml Binding) is that it is very flexible when defining which kind of xml the application would work with, and it automagically creates the needed Java classes so you can parse xml to its corresponding set of classes.

We will work with this simple xml example:


Let's assume this information is sent via http, as xml, and our Java application will store this information into a database.
To get each element from this xml, Jaxb first has to parse the xml into a set of objects.
There would be a Company object which has-a list of employee objects. In turn, these Employee objects has-a Number object (which could be a simple numerical type), has-a Name object too. Unlike Company, note that Employee has only one Number and one Name object.
Lastly, there is a Name object that has-a First and a Last objects, which can simply be Strings.


So, when we parse the xml to objects, we can simply access values as a chain of method calling,

// firstName = "Rafa"
String firstName = company.getEmployees().get(0).getName().getFirst();

We can access all data like this

Employee employee = new Employee();

// Assuming getEmployees() returns a List
List<Employee> employees = company.getEmployees(); for(Employee employee : employees){"Employee number : " + employee.getNumber());"Employee first name  : " + employee.getName().getFirst());"Employee last name : " + employee.getName().getLast());

Which prints:
Employee number : 81
Employee first name : Rafa
Employee last name : Antillon
Employee number : 007
Employee first name : James
Employee last name : Bond

Wow this is super cool! But what if the xml we receive is not valid?

Fret not, Jaxb can validate the type of xml we receive using schema files, or XSDs.

XSD is yet another xml with the rules of which kind of xml elements we will accept, in what order and in which quantity.

Rafael A.

Rafael, born in the city of Monclova and raised in Monterrey, naturally blended into the Inflection Point’s blogger community since he is an avid redditor with almost 10k karma. He has worked with the Java environment and led teams around this language for 15 years and has become a super-cool friend at our community. He is a board-game geek and Lego fan with a Master's degree in Science Computer Technologies.