Aug 032017

ADM

JAXB Hello World Marshalling/Unmarshalling Example

  • 03 August 2017
  • ADM

 

 

 

JAXB Hello World Marshalling/Unmarshalling Example - images/logos/java.jpg

 

Java Architecture for XML Binding (JAXB) is a software framework that allows mapping Java classes to XML and back.

There are two important processes offered by the JAXB framework:

  • marshal - the process to convert a Java object into an XML file.
  • unmarshal - the process to convert XML content into a Java Object.

The following table describes the default data type bindings between the XML data types and Java data types.

XML Schema Type Java Data Type
xsd:unsignedShort int
xsd:unsignedLong java.math.BigDecimal
xsd:unsignedInt long
xsd:unsignedByte short
xsd:time javax.xml.datatype.XMLGregorianCalendar
xsd:string java.lang.String
xsd:short short
xsd:QName javax.xml.namespace.QName
xsd:positiveInteger java.math.BigInteger
xsd:NOTATION javax.xml.namespace.QName
xsd:long long
xsd:integer java.math.BigInteger
xsd:int int
xsd:hexBinary byte[]
xsd:g javax.xml.datatype.XMLGregorianCalendar
xsd:float float
xsd:duration javax.xml.datatype.Duration
xsd:double double
xsd:decimal java.math.BigDecimal
xsd:dateTime javax.xml.datatype.XMLGregorianCalendar
xsd:date javax.xml.datatype.XMLGregorianCalendar
xsd:byte byte
xsd:boolean boolean
xsd:base64Binary byte[]
xsd:anySimpleType java.lang.String
xsd:anySimpleType java.lang.Object

Class annotation

There are three annotations used:

  • XmlRootElement - this is added to the class and will map the class to the XML;
  • XmlElement - is used to methods or property and will map the property to an XML element.
  • XmlAttribute - is used to methods or property and will map the property to an XML attribute.

For this article will choose a simple User class to be marshaled and unmarshaled.

package com.admfactory.jaxb;

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

@XmlRootElement
public class User {

    private int id;
    private String name;
    private String address;
    private String email;

    public int getId() {
	return id;
    }

    @XmlAttribute
    public void setId(int id) {
	this.id = id;
    }

    public String getName() {
	return name;
    }

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

    public String getAddress() {
	return address;
    }

    @XmlElement
    public void setAddress(String address) {
	this.address = address;
    }

    public String getEmail() {
	return email;
    }

    @XmlElement
    public void setEmail(String email) {
	this.email = email;
    }

    @Override
    public String toString() {
	return "User [id:" + id + ",name: " + name + ", email:" + email + ", address: " + address + "]";
    }
}

Object to XML

To convert the object to XML the marshaller.marshal() method is used.

package com.admfactory.jaxb;

import java.io.File;

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

public class JAXBToXML {
    public static void main(String[] args) {
	System.out.println("JAXB Object to XML example");
	System.out.println();
	User user = new User();
	user.setAddress("John Islip, London");
	user.setEmail("john@example.com");
	user.setId(33);
	user.setName("John Doe");
	try {

	    JAXBContext context = JAXBContext.newInstance(User.class);
	    Marshaller marshaller = context.createMarshaller();

	    /** output the XML in pretty format */
	    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

	    /** display the output in the console */
	    marshaller.marshal(user, System.out);
	    
	    /** put the XML to the file - will be used by the unmarshal example */
	    marshaller.marshal(user, new File("user.xml"));

	} catch (JAXBException e) {
	    e.printStackTrace();
	}
    }
}

Output

JAXB Object to XML example

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user id="33">
    <address>John Islip, London</address>
    <email>john@example.com</email>
    <name>John Doe</name>
</user>

XML to Object

To convert the XML file to object the marshaller.unmarshal() method is used.

package com.admfactory.jaxb;

import java.io.File;

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

public class JAXBToObject {
    public static void main(String[] args) {
	System.out.println("JAXB XML to Object example");
	System.out.println();

	try {

	    JAXBContext context = JAXBContext.newInstance(User.class);

	    Unmarshaller unmarshaller = context.createUnmarshaller();
	    User user = (User) unmarshaller.unmarshal(new File("user.xml"));
	    System.out.println(user);

	} catch (JAXBException e) {
	    e.printStackTrace();
	}
    }
}

Output

JAXB XML to Object example

User [id:33,name: John Doe, email:john@example.com, address: John Islip, London]

 

 

 

References