Showing posts with label JAXB. Show all posts
Showing posts with label JAXB. Show all posts

Thursday, July 25, 2013

How to convert XML to Java object and vice versa in runtime?

Well the obvious choice would be to use JaxB, due to its familiarity. Let us see it with a simple example. JAXB is governed by the same team as Glassfish. JAXB comes by default with JDK1.6+. JAXB stands for Java Architecture for XML Binding.

XML has become a common form of data storage in many applications and for webservices. There are two common operations that are required while dealing with XML from Java. Read an xml file and convert it to a Java object, and write a Java object to an xml file.

JAXB does both the above said operations and those operations are called as
  1. Marshalling – Convert a Java object into a XML.
  2. Unmarshalling – Convert a XML into a Java Object.
Let us see this with an example,
Marshalling: Converting Java object to XML
Its a simple process, lets see this with a sample program
Output
While running the above example you will run into the following exception,

Exception in thread "main" javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "com.ananth.samples.Employee" as an element because it is missing an @XmlRootElement annotation]
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:317)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:243)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:75)
at com.ananth.samples.Java2XMLExample.main(Java2XMLExample.java:20)
Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "com.ananth.samples.Employee" as an element because it is missing an @XmlRootElement annotation
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:216)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:286)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:462)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:314)
... 3 more

To fix the above exception you have to add an @XmlRootElement annotation to your Employee Pojo.
Instead of System.out you can point to the File to which you want to write the output during jaxbMarshaller.marshal(employee, file);. Once make the change while rerunning the sample will give your the following output.
UnMarshalling: Converting XML to Java object
Its a simple process, lets see this with a sample program
Now, I had a question why do I should I have a @XmlRootElement annotation in my pojo.  Cant I do I deal with my existing POJOs of my application?
The answer is you cant do it with JAXB but there are other options like XStream. I have referred their two minutes tutorial and it really look me only 2 minutes to try it out.
Add the XStream maven dependency and after that it is as easy as calling toXML and fromXML methods of XStream object.


Wednesday, February 22, 2012

How to avoid namespace using JAXB

After banging several times to eliminate namespace from the generated XML found this useful URL. Gives a detail explanation to understand JAXB namespaces.