Welcome to Java4u

A Single Place for all Java Resources

Looking for something?

Subscribe to this blog!

Receive the latest posts by email.

.Just enter your email below if you want to subscribe!

Email

Thursday, July 3, 2014

Unmarshalling and Marshalling

Unmarshalling is the process of binding the XML document using JAXB compiler and generating mapping java classes then constructing the instances with values available in XML document.

Marshalling is the reverse process of it. First we construct the instances and then write a XML document using the constructed instances.

In both the cases, we need create POJO classes first. If we have the XML schema that can be used to generate those classes using a JAXB binding compiler. If the XML schema is not available, then we should manually code those POJOs. Only then we can do Marshalling or Unmarshalling in an application.

Marshalling and Unmarshalling can be done with other sources. That means, the XML need not be in file form. It can be a InputStream object, a URL, a DOM node, SAXSource.


To Unmarshal 
  1. Create POJOs or bind the schema and generate the classes.
  2. Create a JAXBContext object. (javax.xml.bind.JAXBContext)
  3. Create an Unmarshaller object. (javax.xml.bind.Unmarshaller)
  4. Call the unmarshal method.
  5. Use the get methods available in schema-genearated classes to access the values.
 
 
To Marshal
Marshalling provides a client application the ability to convert a JAXB-derived Java object tree back into XML data. By default, the Marshaller uses UTF-8 encoding when generating XML data. Client applications are not required to validate the Java content tree before marshalling. There is also no requirement that the Java content tree be valid with respect to its original schema to marshal it back into XML data

  1. Create POJOs or bind the schema and generate the classes.
  2. Create the content tree by using set methods.
  3. Create a JAXBContext object. (javax.xml.bind.JAXBContext)
  4. Create a Marshaller object. (javax.xml.bind.Marshaller)
  5. Call the marshal method to persist the created content tree as XML document. 
 
 
Validation:
It is the process of verifying that an XML document meets all the constraints expressed in the schema. JAXB 1.0 provided validation at unmarshal time and also enabled on-demand validation on a JAXB content tree. JAXB 2.0 only allows validation at unmarshal and marshal time.

 
Advantages :
  1. JAXB requires a DTD.
  2. Using JAXB ensures the validity of your XML.
  3. A JAXB parser is actually faster than a generic SAX parser.
  4. A tree created by JAXB is smaller than a DOM tree.
  5. It’s much easier to use a JAXB tree for application-specific code.
  6. You can modify the tree and save it as XML.
Dis-Advantages:
  1. JAXB requires a DTD.
  2. Hence, you cannot use JAXB to process generic XML (for example, if you are writing an XML editor or other tool).
  3. You must do additional work up front to tell JAXB what kind of tree you want it to construct.
  4. But this more than pays for itself by simplifying your application. 

0 comments: