How to convert an XML file into object using JAXB?

In this code snippet you can learn how to convert or unmarshall an XML file into it corresponding POJO. The steps on unmarshalling XML to object begin by creating an instance of JAXBContext. With the context object we can then create an instance of Unmarshaller class. Using the unmarshall() method and pass an XML file will give us the target POJO as the result.

Let’s see the code snippet below:

package org.kodejava.example.jaxb;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;

public class JAXBXmlToObject {
    public static void main(String[] args) {
        try {
            File file = new File("Track.xml");
            JAXBContext context = JAXBContext.newInstance(Track.class);

            Unmarshaller unmarshaller = context.createUnmarshaller();
            Track track = (Track) unmarshaller.unmarshal(file);

            System.out.println("Track = " + track);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

Here is the context of Track.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<track id="2">
    <title>She Loves You</title>
</track>

How do I define the XML element order in JAXB?

In this code snippet you will learn how to define the order of XML element generated by the JAXB API. To define the element order we need to use the @XmlType annotation in our POJO. This annotation propOrder is where we define what element should come first and which element should be place at the end.

The XML generated by the marshaller below will have the following order: street, city, province, zipCode and country as the last XML element.

package org.kodejava.example.jaxb;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "customerAddress")
@XmlType(propOrder = {"street", "city", "province", "zipCode", "country"})
public class Address {
    private String street;
    private String city;
    private String province;
    private String country;
    private String zipCode;

    @XmlElement
    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    @XmlElement
    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @XmlElement
    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    @XmlElement
    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @XmlElement(name = "postCode")
    public String getZipCode() {
        return zipCode;
    }

    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    @Override
    public String toString() {
        return "Address{" +
                "street='" + street + ''' +
                ", city='" + city + ''' +
                ", province='" + province + ''' +
                ", country='" + country + ''' +
                ", zipCode='" + zipCode + ''' +
                '}';
    }
}

Now, let’s create a program to run the marshalling process of the Address POJO to convert it into XML document:

package org.kodejava.example.jaxb;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class JAXBElementOrder {
    public static void main(String[] args) {
        Address address = new Address();
        address.setStreet("Sunset Road");
        address.setCity("Denpasar");
        address.setProvince("Bali");
        address.setCountry("Indonesia");
        address.setZipCode("800000");

        try {
            JAXBContext context = JAXBContext.newInstance(Address.class);

            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(address, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

And it will give you the following output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customerAddress>
    <street>Sunset Road</street>
    <city>Denpasar</city>
    <province>Bali</province>
    <country>Indonesia</country>
    <postCode>800000</postCode>
</customerAddress>

How do I change the XML root element name in JAXB?

In the following code snippet you will learn how to change the default root element name of the XML generated by the JAXB API. By default the name of the class is use as the root element name. To change the root element name we can use the name property of the @XmlRootElement annotation. In the Customer model below we change the root element name into cust.

package org.kodejava.example.jaxb;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "cust")
@XmlType(propOrder = {"id", "name", "address"})
public class Customer {
    private Integer id;
    private String name;
    private Address address;

    @XmlElement
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement
    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

When we convert this POJO to XML using JAXB API we will get the following result:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cust>
    <id>1</id>
    <name>Johnny Mnemonic</name>
    <address>
        <street>Sunset Road</street>
        <city>Denpasar</city>
        <province>Bali</province>
        <postCode>800000</postCode>
        <country>Indonesia</country>
    </address>
</cust>

How do I get attributes of element during SAX parsing?

This example show you how to get the attributes of elements in an XML file using the SAX parser.

package org.kodejava.example.xml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.InputStream;

public class SAXElementAttribute {
    public static void main(String[] args) {
        SAXElementAttribute demo = new SAXElementAttribute();
        demo.run();
    }

    private void run() {
        try {
            //
            // Create SAXParserFactory instance and a SAXParser
            //
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();

            //
            // Get an InputStream to the elements.xml file and parse
            // its contents using the SAXHandler.
            //
            InputStream is =
                    getClass().getResourceAsStream("/elements.xml");
            DefaultHandler handler = new SAXHandler();
            parser.parse(is, handler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    class SAXHandler extends DefaultHandler {
        @Override
        public void startElement(String uri, String localName,
                                 String qName, Attributes attributes)
                throws SAXException {

            int attributeLength = attributes.getLength();
            if ("person".equals(qName)) {
                for (int i = 0; i < attributeLength; i++) {
                    //
                    // Get attribute names and values
                    //
                    String attrName = attributes.getQName(i);
                    String attrVal = attributes.getValue(i);
                    System.out.print(attrName + " = " + attrVal + "; ");
                }
                System.out.println("");
            }
        }
    }
}

The elements.xml file is as follow:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <persons>
        <person name="Foo" age="25"/>
        <person name="Bar" age="22"/>
    </persons>
</root>

How do I handle error when parsing an XML file using SAX?

The ErrorHandler interface implemented by the org.xml.sax.helpers.DefaultHandler class provides some methods for error handling mechanism in SAX parsing. The methods are warning(), error(), and fatalError().

package org.kodejava.example.xml;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.InputStream;

public class SAXErrorHandlerDemo {
    public static void main(String[] args) throws Exception {
        SAXErrorHandlerDemo demo = new SAXErrorHandlerDemo();
        demo.run();
    }

    public void run() throws Exception {
        //
        // Creates the SAXParserFactory and SAXParser instance.
        //
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();

        //
        // Parse the error.xml file using MySAXHandler as the 
        // DefaultHandler implementation.
        //
        InputStream is = getClass().getResourceAsStream("/error.xml");
        DefaultHandler handler = new MySAXHandler();
        parser.parse(is, handler);
    }

    //
    // Override the error handling methods defined by the ErrorHandler
    // interface. This method will handler exceptions thrown by the
    // parsing process.
    //
    class MySAXHandler extends DefaultHandler {
        @Override
        public void warning(SAXParseException e) throws SAXException {
            System.out.println("warning   : " + e.getMessage());
        }

        @Override
        public void error(SAXParseException e) throws SAXException {
            System.out.println("error     : " + e.getMessage());
        }

        @Override
        public void fatalError(SAXParseException e) throws SAXException {
            System.out.println("fatalError: " + e.getMessage());
        }
    }
}