JAXB Marshalling / UnMarshalling using Generics:
Java Architecture for XML Binding used to convert XML to JAVA POJO objects. Traditional way is to write SAX / DOM parser and convert that into JAVA Objects, with JAXB the conversion is done in two stages.
1. Generation of POJO classes from XML Schema Definition
2. Using JAXB UnMarshaller to convert the XML to POJO objects.
Sample XML to unmarshall the xml to JAVA POJO is,Java Architecture for XML Binding used to convert XML to JAVA POJO objects. Traditional way is to write SAX / DOM parser and convert that into JAVA Objects, with JAXB the conversion is done in two stages.
1. Generation of POJO classes from XML Schema Definition
2. Using JAXB UnMarshaller to convert the XML to POJO objects.
public Orders unMarshal(String content) { try { JAXBContext jc = JAXBContext.newInstance( Orders.class ); Unmarshaller u = jc.createUnmarshaller(); return (Orders) u.unmarshal(new StreamSource( new StringReader( content )), Orders.class).getValue(); } catch (JAXBException e) { log.error(String.format("Exception while unmarshalling: %s", e.getMessage())); } return null; }In the above code, Orders.class is a generated POJO from below XML schema definition,
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="2.0"> <xsd:element name="Orders"> <xsd:complexType> <xsd:sequence> <xsd:element name="Order" type="OrderType" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name="OrderType"> <xsd:sequence> <xsd:element name="symbol" type="xsd:string"/> <xsd:element name="size" type="xsd:int"/> <xsd:element name="executionType" type="ExecutionType"/> </xsd:sequence> </xsd:complexType> <xsd:simpleType name="ExecutionType"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="MARKET_ORDER"/> <xsd:enumeration value="LIMIT_ORDER"/> <xsd:enumeration value="STOP_ORDER"/> <xsd:enumeration value="STOP_LIMIT_ORDER"/> </xsd:restriction> </xsd:simpleType> </xsd:schema>If we have to Unmarshall another xml schema CancelOrders,
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="2.0"> <xsd:element name="CancelOrder"> <xsd:complexType> <xsd:sequence> <xsd:element name="orderNumber" type="xsd:int"/> <xsd:element name="reason" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
then the code to unmarshall the object would be,
public CancelOrders unMarshal(String content) { try { JAXBContext jc = JAXBContext.newInstance( CancelOrders.class ); Unmarshaller u = jc.createUnmarshaller(); return (Orders) u.unmarshal(new StreamSource( new StringReader( content )), CancelOrders.class).getValue(); } catch (JAXBException e) { log.error(String.format("Exception while unmarshalling: %s", e.getMessage())); } return null; }Using generics, the above Unmarshalling JAVA code could be simplified and could be reused as below,
public <T> T unMarshal(String content, Class<T> clasz) { try { JAXBContext jc = JAXBContext.newInstance( clasz ); Unmarshaller u = jc.createUnmarshaller(); return u.unmarshal(new StreamSource( new StringReader( content )), clasz).getValue(); } catch (JAXBException e) { log.error(String.format("Exception while unmarshalling: %s", e.getMessage())); } return null; }The above function uses generics and accepts the xml content as string and the POJO class to be converted as generic class.
Using generics, the same code is reused for any xml content to be converted to their specific POJO objects.
Similarly Marshalling of JAVA pojo's to XML cound done using JAVA Generics as below,
public <T> String marshal(T object) { try { StringWriter stringWriter = new StringWriter(); JAXBContext jc = JAXBContext.newInstance( object.getClass()); Marshaller m = jc.createMarshaller(); m.marshal(object, stringWriter); return stringWriter.toString(); } catch (JAXBException e) { log.error(String.format("Exception while marshalling: %s", e.getMessage())); } return null; }
Downloads:
Full Maven project download
No comments:
Post a Comment