Translate

Unmarshalling an XML file with JAXB

This is a simple example for unmarshalling an XML file.

This is the XML file we are going to unmarshall
<?xml version="1.0" encoding="utf-8"?>
<order>
<order-id>002</order-id>
<price>1000000</price>
<items>
<no-of-items>4</no-of-items>
<itemList>
<item-name>item-a</item-name>
<item-name>item-b</item-name>
<item-name>item-c</item-name>
<item-name>item-d</item-name>
</itemList>
</items>
</order>
view raw order.xml hosted with ❤ by GitHub
This is the schema of the above XML file
<?xml version="1.0" encoding="UTF-8"?>
<schema version="1.0" xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" >
<element name="order">
<complexType>
<sequence>
<element name="order-id" type="string" minOccurs="1" maxOccurs="1"/>
<element name="price" type="string" minOccurs="1" maxOccurs="1"/>
<element name="items" minOccurs="1" maxOccurs="1">
<complexType>
<sequence>
<element name="no-of-items" type="int" minOccurs="1" maxOccurs="1"/>
<element name="itemList" minOccurs="1" maxOccurs="1">
<complexType>
<sequence>
<element name="item-name" type="string" minOccurs="1" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
view raw schema.xsd hosted with ❤ by GitHub
Now let's create Order.java
Here for each complex type I have created an inner class.
And for the element "item-name" which has the maxOccurs=unbounded, I have used a List.
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"orderId",
"price",
"items"
})
@XmlRootElement(name = "order")
public class Order {
@XmlElement(name = "order-id", required = true)
private String orderId;
@XmlElement(name = "price" ,required = true)
private String price;
@XmlElement(name="items", required = true)
private Items items;
public String getOrderId() {
return orderId;
}
public void setOrderId(String value) {
orderId = value;
}
public String getPrice() {
return price;
}
public void setPrice(String value) {
price = value;
}
public Items getItems() {
return items;
}
public void setItems(Items value) {
items = value;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"noOfItems",
"itemList"
})
public static class Items {
@XmlElement(name = "no-of-items")
private int noOfItems;
@XmlElement(name="itemList", required = true)
private ItemList itemList;
public int getNoOfItems() {
return noOfItems;
}
public void setNoOfItems(int value) {
noOfItems = value;
}
public ItemList getItemList() {
return itemList;
}
public void setItemList(ItemList value) {
itemList = value;
}
@XmlAccessorType(XmlAccessType.FIELD)
public static class ItemList {
@XmlElement(name = "item-name", required = true)
private List<String> itemName;
public List<String> getItemName() {
if (itemName == null) {
itemName = new ArrayList<String>();
}
return itemName;
}
}
}
}
view raw Order.java hosted with ❤ by GitHub
Another thing is @XmlAccessorType(XmlAccessType.FIELD) is needed only if the annotated fields inside the class have public setters.  If you don't have public setters for the annotated fields inside the class you don't need to annotate the class.

Now let's write the code for unmarshalling.
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
/**
* Created by manurip on 7/12/15.
*/
public class OrderUnmarshallar {
private static OrderUnmarshallar instance = null;
private OrderUnmarshallar(){
}
public static OrderUnmarshallar getJAXBUnmarshallar(){
if(instance==null){
instance=new OrderUnmarshallar();
}
return instance;
}
public Order unmarshall(File XMLfile){
Order order=null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Order.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
order = (Order) jaxbUnmarshaller.unmarshal(XMLfile);
} catch (JAXBException e) {
e.printStackTrace();
}
return order;
}
public static void main(String[] args) {
Order order=getJAXBUnmarshallar().unmarshall(new File("/path/to/order.xml"));
System.out.println(order.getOrderId());
System.out.println(order.getPrice());
System.out.println(order.getItems().getNoOfItems());
for(String item:order.getItems().getItemList().getItemName()){
System.out.println(item);
}
}
}
  

Google Summer of Code 2015



This year also I'm participating Google Summer of Code with Eclipse foundation.

My project is Implementing translation between Proj4 and Well-Known-Text in Geotrellis which is a high performance geoprocessing engine and programming toolkit.

Project description in Google Melange can be found here.