This is the XML file we are going to unmarshall
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} |
Now let's write the code for unmarshalling.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |