Marshalling


Marshalling means producing a stream of byte which contain enough information to be able to re-build the object. This make no impact on the original object, because it is a read-only operation.

JAXB


JAXB stand for Java Architecture for XML Binding. Jaxb allows us to map Java classes to XML representations. JAXB can be used Java to marshal java object into xml or/and to unmarshal XML back into Java objects.

I used these 2 jar for this example. You can download latest version of jar and add it to your class path.

  1. jaxb-api-2.1.jar
  2. jaxb-impl-2.1.jar

Marshalling to a File


Marshalling to a SAX ContentHandler


Marshalling to a DOM Node


Marshalling to a java.io.OutputStream


Marshalling to a java.io.Writer


Marshalling to a javax.xml.transform.SAXResult


Marshalling to a javax.xml.transform.DOMResult


Marshalling to a javax.xml.transform.StreamResult


Marshalling to a javax.xml.stream.XMLStreamWriter


Marshalling to a javax.xml.stream.XMLEventWriter


Marshalling example


Here is BookCatalog.java which we are going to marshalled into xml using Jaxb apis.

BookCatalog.java

Book.java

Source code of MarshalingMain.java is given below.

MarshalingMain.java

Result

Share & Like

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.