main banner

Development

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

In the last part we reviewed how to access get data from an xml file and use it programmatically. The data we receive has to be placed in according objects, such as Company, Employee, and so on, and Jaxb helps us create classes to represent these objects.



To do so, Jaxb uses XSD files. XSD stands for XML schema definition, and they define which kind of xml we are expecting, so we can create the appropriate classes.

An XSD for the Company example would look like this:

<xs:element name="Company">
<xs:complexType>
  <xs:sequence>
   <xs:element ref="Employee" maxOccurs="unbounded" minOccurs="0"/>
  </xs:sequence>
 </xs:complexType>
</xs:element>

This part tells us that we expect an xml element called Company, which could have an Employee Element. It expects from 0 to unbounded elements, that is 0 to infinity Employee elements.

Likewise, the Employee element would be:

<xs:element name="Employee">
<xs:complexType>
  <xs:sequence>
   <xs:element name="Number" maxOccurs="1" minOccurs="1" type="xs:int"/>
<xs:element ref="Name" maxOccurs="1" minOccurs="1"/>
 </xs:sequence>
</xs:complexType>
</xs:element>

This would tell Jaxb that the Employee element would be composed of a Number and a Name Element. Employee also needs to have at least 1 Number and Name  and at most 1 too, meaning it requires both elements and only have one of each.

We then would have to define Name element like we did for Company and Employee, but since  Number is a simple numeric value, we just define it as an int.

So when Jaxb generates the classes, it will generate it to accommodate the values. The Java classes are generated using xjc:

C:\JaxbArticle>"%JAVA_HOME%/bin/xjc.exe" jaxbArticle.xsd

This is an extract of how it would generate Company class:

 @XmlRootElement(name = "Company")
public class Company {
    @XmlElement(name = "Employee")
    protected List<Employee> employee;
    public List<Employee> getEmployee() {
        if (employee == null) {
            employee = new ArrayList<Employee>();
        }
        return this.employee;
    }

This part tells us that we expect an xml element called Company, which could have an Employee Element. It expects from 0 to unbounded elements, that is 0 to infinity Employee elements.

Likewise, the Employee element would be:

 <xs:element name="Employee">
<xs:complexType>
  <xs:sequence>
   <xs:element name="Number" maxOccurs="1" minOccurs="1" type="xs:int"/>
<xs:element ref="Name" maxOccurs="1" minOccurs="1"/>
 </xs:sequence>
</xs:complexType>
</xs:element> 

This would tell Jaxb that the Employee element would be composed of a Number and a Name Element. Employee also needs to have at least 1 Number and Name  and at most 1 too, meaning it requires both elements and only have one of each.

We then would have to define Name element like we did for Company and Employee, but since  Number is a simple numeric value, we just define it as an int.

So when Jaxb generates the classes, it will generate it to accommodate the values.
The Java classes are generated using xjc:

C:\JaxbArticle>"%JAVA_HOME%/bin/xjc.exe" jaxbArticle.xsd

This is an extract of how it would generate Company class:

@XmlRootElement(name = "Company")
public class Company {

    @XmlElement(name = "Employee")
    protected List<Employee> employee;

    public List<Employee> getEmployee() {
        if (employee == null) {
            employee = new ArrayList<Employee>();
        }
        return this.employee;
    }

Since Company contains 0 to infinity Employees, it creates a List of Employees inside Company.

@XmlRootElement(name = "Employee")
public class Employee {

    @XmlElement(name = "Number")
    protected int number;

And since Employee has exactly one Number, it creates a single field value.

This XSD also helps us validate if we are using a correct XML file, if Jaxb tries to parse an xml that is not expected, it throws an error. This way we can be sure that if Jaxb correctly parses an XML, it means we are working with a valid and expected XML

>>>>>>>>>> I can put some code of how to parse XML but it is ugly and easily googlable.<<<<<<<<<<

So, in short, Jaxb is a very helpful tool either if you want to parse an XML file, or if you want to save java objects to an XML file.


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.

Articles