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!


OAuth 2.0

OAuth 2.0 is an open authorization framework and mainly focuses on authorization flows fand secures access to many well-known web APIs.

Java 8 features

The key features of JDK 8 are Project Lambda (JSR 335), the Nashorn JavaScript Engine, a new Date and Time API (JSR 310), a set of Compact Profiles and the removal of the "permanent generation" from the HotSpot Java Virtual Machine (JVM). A complete list of the new features and capabilities of JDK 8 is available.

Lambda Expression

A lambda expression represents an anonymous function. It comprises of a set of parameters, a lambda operator (->) and a function body.


SOAP : Web service use XML messages that follow the Simple Object Access Protocol (SOAP) standard.
RESTful : Web service, often better integrated with HTTP than SOAP-based services are, do not require XML messages or WSDL service–API definitions.

Softwares Engineering / SDLC

Software engineers apply the principles of engineering to the design, development, maintenance, testing, and evaluation of the software and systems that make computers or anything containing software work.

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. 
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.
  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. 

JAXB : Architecture & Binding Process

XML Schema : An XML schema is a definition file for a XML document & uses XML syntax to describe the relationships among elements, attributes and entities in an XML document. It maintains the rule/syntax for a XML document. An XML document need not always have a schema. While using JAXB XML schema is not a mandatory requirement. Process can start with java pojo classes. But it is convenient to have associated XML schema so that we can use a binding compiler and generate the java classes.

Binding Customizations : By default, the JAXB binding compiler binds Java classes and packages to a source XML schema based on rules. In most cases, the default binding rules are sufficient to generate a robust set of schema-derived classes from a wide range of schemas.

Binding Compiler : The JAXB binding compiler is the core of the JAXB processing model. Its function is to transform, or bind, a source XML schema to a set of JAXB content classes in the Java programming language.

Implementation of javax.xml.bind
: The JAXB binding framework implementation is a runtime API that provides interfaces for unmarshalling, marshalling, and validating XML content in a Java application. The binding framework comprises interfaces in the javax.xml.bind package.

Schema-Derived Classes: These are the schema-derived classes generated by the binding JAXB compiler. The specific classes will vary depending on the input schema.

Java Application : In the context of JAXB, a Java application is a client application that uses the JAXB binding framework to unmarshal XML data, validate and modify Java content objects, and marshal Java content back to XML data.

XML Input Documents: XML content that is unmarshalled as input to the JAXB binding framework -- that is, an XML instance document, from which a Java representation in the form of a content tree is generated.

XML Output Documents : XML content that is marshalled out to an XML document. In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema.

Binding Process :
Generate classes: An XML schema is used as input to the JAXB binding compiler to generate JAXB classes based on that schema.

Compile classes
: All of the generated classes, source files, and application code must be compiled.

UnMarshal: XML documents written according to the constraints in the source schema are unmarshalled by the JAXB binding framework.

Note : JAXB also supports unmarshalling XML data from sources other than files/documents, such as DOM nodes, string buffers, SAX Sources, and so forth.

Generate content tree: The unmarshalling process generates a content tree of data objects instantiated from the generated JAXB classes; this content tree represents the structure and content of the source XML documents.

Validate (optional) : The unmarshalling process optionally involves validation of the source XML documents before generating the content tree. Note that if you modify the content tree in Step 6, below, you can also use the JAXB Validate operation to validate the changes before marshalling the content back to an XML document.

Process content : The client application can modify the XML data represented by the Java content tree by means of interfaces generated by the binding compiler.

Marshal : The processed content tree is marshalled out to one or more XML output documents. The content may be validated before marshalling.

JAXB : Introduction & Features

JAXB is an acronym of Java Architecture for XML Binding. 
  • JAXB provides API to access and process a XML document.
  • We can read or write XML files using JAXB.
  • SAX and DOM are generic XML parsers& they will parse any well-structured XML. 
  • JAXB creates a parser that is specific to your DTD.
  • A JAXB parser will parse only valid XML (as defined by your DTD).DOM and JAXB both produce a tree in memory.
  • DOM produces a generic tree, everything is a Node.JAXB produces a tree of Objects with names and attributes as described by your DTD.
Features of JAXB :
  • Ease of development
  • Typed access to XML content
  • Leverage Java SE 5 features
  • 100% XML Schema support
  • Java classes to XML Schema binding
  • Data binding for Java API for XML Web Services (JAX-WS)
  • Schema evolution
  • Provide optional on-demand validation of XML documents
  • Hides the complexity of DOM/SAX APIs
  • Is portable (A JAXB application should be able to utilize different JAXB implementations by just regenerating the schema classes and doesn't require change to the actual application code.)