<guid isPermaLink="false">tag:blogger.com,1999:blog-1373851816248476399.post-4373405763874058313</guid>
<pubDate>Sat, 16 Apr 2011 14:42:00 +0000</pubDate>
<atom:updated>2011-04-19T10:16:50.837+05:30</atom:updated>
<category domain="http://www.blogger.com/atom/ns#">Data Transformer</category>
<title>Best Practices for XML and POJO Binding</title>
<description><div dir="ltr" style="text-align: left;" trbidi="on"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span class="Apple-style-span" style="font-size: 13px; line-height: 15px;"></span></span><br />
<span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"></span><br />
<div style="font-style: normal; font-weight: normal; line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: black;"><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-size: x-small;">Here goes few basic guidelines to choose from different Data Binding Technologies :</span></span></span></span></div><div style="font-style: normal; font-weight: normal; line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="font-style: normal; line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: black;"><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-size: x-small;"><b>XMLBeans :</b></span></span></span></span></div><div style="line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: black;"><span style="font-size: x-small;"><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-style: normal;"><span style="font-weight: normal;">XMLBeans</span></span></span><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-style: normal;"><span style="font-weight: normal;"> is the best candidate - when </span><b>we just depend upon underlying XML for the actual data </b><span style="font-weight: normal;">and don't really need to manipulate the Java code or modify the xml. </span></span></span></span></span> </span></div><div style="font-style: normal; font-weight: normal; line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: black;"><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-size: x-small;">Say we just need to convert an WSDL into Java Code and then use the generated Java API for aaceesing WSDL Operations and Interfaces.</span></span></span></span></div><div style="font-style: normal; font-weight: normal; line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="font-style: normal; line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: black;"><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-size: x-small;">XMLBeans actually stores the entire set of parsed events that represent the document in memory. XMLBeans is <b>very fast for just reading and writing documents.</b> It is<b> really XML-Centric</b>.</span></span></span></span></div><div style="font-style: normal; font-weight: normal; line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: #222222;"><span style="font-family: Arial, sans-serif;"><span style="font-size: x-small;"><i>"XMLBeans is based on an efficient XML token stream, and it keeps underlying XML infoset intact. Since XMLBeans keeps the data in memory as XML, overhead of unmarshalling and marshalling is reduced. </i></span></span></span></span></div><div style="line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: #222222;"><span style="font-family: Arial, sans-serif;"><span style="font-size: x-small;"><i>To address varying data access and data transformation requirements, XMLBeans provides a flexibility of navigating through xml data using xPath or xQuery, and manipulating xml data using either xml cursor or XMLBeans generated classes."</i></span></span></span></span></div><div style="font-style: normal; font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="font-style: normal; font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: #222222;"><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-size: x-small;">XMLBeans performs poorly in terms of handling memory because it stores a live mapping of objects and underlying XML Document in memory.</span></span></span></span></div><div style="font-style: normal; font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="font-style: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: #222222;"><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-size: x-small;"><b>JiBX:</b></span></span></span></span></div><div style="font-style: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: #222222;"><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-size: x-small;">If we really want to manipulate <b>the way XML Data should be mapped to Java Objects</b>, JiBX scores high !</span></span></span></span></div><div style="font-style: normal; font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: #222222;"><span style="font-family: Verdana, sans-serif;"><span style="font-size: x-small;"><span style="font-family: arial, verdana, helvetica, sans-serif;">JiBX uses mapped bindings to automatically generate code and does not care much about the XML Schema Support. I</span><span style="font-family: arial, verdana, helvetica, sans-serif;"><b>t is </b></span><span style="color: black;"><span style="font-family: arial, sans-serif;"><b>a </b></span></span><span style="color: black;"><span style="font-family: arial, sans-serif;"><i><b>Java-centric</b></i></span></span><span style="color: black;"><span style="font-family: arial, sans-serif;"><b> approach to data binding</b></span></span><span style="color: black;"><span style="font-family: arial, sans-serif;">. It uses ByteCode enhancement to add XML generation and parsing support directly into the bytecode of Java Class. </span></span></span></span></span> </span></div><div style="font-style: normal; font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="font-style: normal; font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: #222222;"><span style="font-family: Verdana, sans-serif;"><span style="font-size: x-small;"><span style="color: black;"><span style="font-family: arial, verdana, helvetica, sans-serif;">"It does not tie the Java class to the structure of the XML document. Just because an element lives within another element in the document, does not imply that the data contained in the sub element must be an equivalent level down in the Java structure. This allows the deep XML structure (which makes sense in XML) to be mapped easily to a more shallow, but not necessarily flat, structure (which makes sense in the Java object model).</span></span><span style="font-family: arial, verdana, helvetica, sans-serif;">"</span></span></span></span></span></div><div style="font-style: normal; font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: #222222;"><span style="font-family: Verdana, sans-serif;"><span style="font-size: x-small;"><span style="font-family: arial, verdana, helvetica, sans-serif;">It uses fast </span><span style="color: black;"><span style="font-family: arial, verdana, helvetica, sans-serif;">Pull-Parser and has a very compact Runtime Distribution.</span></span></span></span></span></span></div><div style="font-style: normal; font-weight: normal; line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: black;"><span style="font-family: arial, sans-serif;"><span style="font-size: x-small;">JiBX isolates XML document formats from Java language object structures.</span></span></span></span></div><div style="font-style: normal; font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="font-style: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: #222222;"><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-size: x-small;"><b>JAXB:</b></span></span></span></span></div><div style="font-style: normal; font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: #222222;"><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-size: x-small;">JAXB generates code from Schema. Very much XML dependent, but still provides ways to manipulate mappings through annotations. Its easy to work but difficult to maintain and is not as elegant as XMLBeans or JiBX. To some extent its between Java-Centric JiBX and XML-Centric XMLBeans. </span></span></span> </span></div><div style="font-style: normal; font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: #222222;"><span style="font-family: Verdana, sans-serif;"><span style="font-size: x-small;"><i><span style="font-family: arial, verdana, helvetica, sans-serif; font-weight: normal;">" </span>JAXB among other things allows you to <b>customize binding through schema annotation</b> such that generated classes are more than just XML containers - they can represent objects with real behavior (and ability to be instantiated from XML)."</i></span></span></span></span></div><div style="font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="font-style: normal; font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: #222222;"><span style="font-family: Verdana, sans-serif;"><span style="font-size: x-small;">JAXB has issues with <span style="color: black;"><span style="font-family: arial, verdana, helvetica, sans-serif;">deeply nested (and large) xsds. It generates too many classes and for large xmls simply grow unmangeable. (</span></span>JAXB does not make any use of inner classes while generatiing code). </span></span></span> </span></div><div style="font-style: normal; font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="font-style: normal; font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: black;"><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-size: x-small;">JAXB generated classes can not parse xmls, whereas XMLBeans allow parsing underlying xml. (its more xml centric)</span></span></span></span></div><div style="font-style: normal; font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="font-style: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: black;"><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-size: x-small;"><b>EMF :</b></span></span></span></span></div><div style="font-style: normal; font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: black;"><span style="font-family: Arial, sans-serif;"><span style="font-size: x-small;"><span style="font-family: arial, verdana, helvetica, sans-serif;">EMF produces an Ecore model given an XML Schema and then uses template-based generator technology to generate a rich Java API (of hand written quality).The XML Schema to Ecore conversion can be tailored, the templates used to generate the Java API can be tailored, and the resulting Java API can be tailored.The generator supports merging regeneration so that it will preserve your hand written changes.In other words, EMF is far richer and more flexible, and supports a broader subset of XML Schema.</span></span></span></span></span></div><div style="font-style: normal; font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="font-style: normal; font-weight: normal; line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: #222222;"><span style="font-family: Verdana, sans-serif;"><span style="font-size: x-small;"><b>XStream</b> - simplest to use</span></span></span></span></div><div style="line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: black;"><span style="font-family: helvetica, arial, sans-serif;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">. </span></span></span></span></span><a href="http://www.extreme.indiana.edu/xgws/xsoap/xpp/mxp1/index.html"><span style="color: #006600;"><span style="font-family: helvetica, arial, sans-serif;"><span style="font-size: x-small;"><span style="font-style: normal;"><u><span style="font-weight: normal;">XPP3</span></u></span></span></span></span></a><span style="color: black;"><span style="font-family: helvetica, arial, sans-serif;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;"> is a </span><b>very fast XML pull-parser implementation</b><span style="font-weight: normal;">. User is also free to choose any other parser like JAXP DOM inside XStream.</span></span></span></span></span></span></div><div style="line-height: 0.2in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="font-style: normal; line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: black;"><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-size: x-small;">XStream is designed fo<b>r configurationless serialization</b>. This makes it painless to serialize any type of objects, without the need for mappings. Ideal for things like persistence, configuration and over-the-wire transports. However, because there are no mappings, you have little control of how the serialized object is represented.</span></span></span></span></div><div style="line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: black;"><span style="font-size: x-small;"><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-style: normal;"><span style="font-weight: normal;">** solution : </span></span></span><span style="font-family: arial, sans-serif;"><span style="font-style: normal;"><span style="font-weight: normal;"> </span></span></span><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-style: normal;"><span style="font-weight: normal;">using XStream this way to process SOAP messages based on complex XML schemas. So XSLT acts as your mapping definition and it is much more powerful than plain declarative "binding" mappings, since it is a full-blown templating language.</span></span></span></span></span></span></div><div style="font-style: normal; font-weight: normal; line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="font-style: normal; font-weight: normal; line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: black;"><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-size: x-small;">** custom converters are powerful</span></span></span></span></div><div style="font-style: normal; font-weight: normal; line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: black;"><span style="font-family: 'Times New Roman';"><span style="font-size: x-small;"><span style="font-family: arial, verdana, helvetica, sans-serif;">** </span><span style="color: #40348c;"><span style="font-family: tahoma, arial, sans-serif;"><b>XStream: how to serialize objects to non XML formats</b></span></span></span></span></span></span></div><div style="font-style: normal; font-weight: normal; line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="font-style: normal; font-weight: normal; line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: black;"><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-size: x-small;">Xstream can marshal to / unmarshal from not just xml but also json, tree structure etc.</span></span></span></span></div><pre class="western" style="font-style: normal; font-weight: normal; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: black;"><span style="font-family: arial, sans-serif;"><span style="font-size: x-small;">The modular design allows other output formats. XStream ships currently with JSON support and morphing.</span></span></span></span></pre><div style="font-style: normal; font-weight: normal; line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><span style="color: black;"><span style="font-size: x-small;"><span style="font-family: arial, sans-serif;"><span style="font-style: normal;"><span style="font-weight: normal;">So the bottomline - </span></span></span><span style="font-family: arial, verdana, helvetica, sans-serif;"><span style="font-style: normal;"><span style="font-weight: normal;">If you need precise control of how your objects are represented and are happy to spend time defining mappings, use JiBX. Otherwise stick to Xstream.</span></span></span></span></span></span></div><div style="font-style: normal; font-weight: normal; line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="font-style: normal; font-weight: normal; line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="font-style: normal; font-weight: normal; line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;"><br />
</span></div><div style="line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"><span class="Apple-style-span" style="font-family: arial, verdana, helvetica, sans-serif; font-size: small;">Reference : <a href="http://www.ibm.com/developerworks/library/x-databd4/"><span style="color: black;"><span style="font-family: 'Times New Roman';"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">http://www.ibm.com/developerworks/library/x-databd4/</span></span></span></span></span></a></span></div><div style="line-height: 0.16in; margin-bottom: 0in; orphans: 2; widows: 2;"></div></div></description>
<link>http://musings-on-technology.blogspot.com/2011/04/best-practices-for-xml-and-pojo-binding.html</link>
<author>noreply@blogger.com (Kaniska)</author>
<thr:total>0</thr:total>