Skip to Content
package test.jaxbpimapping;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import sourceData.Book;
import sourceData.Details;
import targetData.AdditionalInfo;
import targetData.AuthoredBy;
import targetData.BookData;
import targetData.BookTitle;
import targetData.CurrentEdition;
import targetData.DetailInfo;
import targetData.JournalInfo;
import targetData.PublishedBy;
public class PI_JAXB_Source_To_Target extends AbstractTransformation {
           * Each JAVA Mapping using the 7.1 API will implement the method
           * transform(TransformationInput arg0, TransformationOutput arg1)
           * as opposed to execute(inputStream in, outputStream out) Method
           * in earlier version.
          InputStream is = null;
          @SuppressWarnings({ "unchecked", "unchecked" })
          public void transform(TransformationInput arg0, TransformationOutput arg1)
                              throws StreamTransformationException {
                     * An info message is added to trace. An instance of trace of object is
                     * obtained by calling the getTrace method of class AbstractTransformation
                    getTrace().addInfo("JAVA Mapping PI_JAXB_Source_To_Target is Initiated");
                    is = arg0.getInputPayload().getInputStream();
                    JAXBContext jaxbContext = JAXBContext.newInstance(Book.class);
                    Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
                    /* Uncomment the below line of code in case you want to force a schema level validation of the incoming XML*/
                    /*SchemaFactory schemaFactory = SchemaFactory.newInstance("");
                    Schema schema = schemaFactory.newSchema(new File("BookXSD.xsd"));
                    Book book_source = (Book) unMarshaller.unmarshal(is);
                    getTrace().addInfo("Unmarshall Successful");
                    /*------------- Marshall the Target----------------*/
                    targetData.ObjectFactory factoryTarget = new targetData.ObjectFactory();
                    BookData bookdata = factoryTarget.createBookData();
                    DetailInfo detailinfo = factoryTarget.createDetailInfo();
                    AuthoredBy authoredby = factoryTarget.createAuthoredBy();
                    BookTitle booktitle = factoryTarget.createBookTitle();
                    CurrentEdition currentedition = factoryTarget.createCurrentEdition();
                    AdditionalInfo addInfo = factoryTarget.createAdditionalInfo();
                    PublishedBy publishedby = factoryTarget.createPublishedBy();
                    JournalInfo journalinfo = factoryTarget.createJournalInfo();
                    List<Details> detailsS_List = book_source.getDetails();
                    getTrace().addInfo("Source Book has (" + detailsS_List.size() + ") Details Node");
                    for (int i = 0; i < detailsS_List.size(); i++)
                              Details detailsS = (Details) detailsS_List.get(i);
                              detailinfo = factoryTarget.createDetailInfo();
                              addInfo = factoryTarget.createAdditionalInfo();
                              authoredby = factoryTarget.createAuthoredBy();
                              booktitle = factoryTarget.createBookTitle();
                              currentedition = factoryTarget.createCurrentEdition();
                              publishedby = factoryTarget.createPublishedBy();
                              journalinfo = factoryTarget.createJournalInfo();
                    JAXBContext contextTarget = JAXBContext.newInstance(BookData.class);
                    Marshaller marshallerBookdata = contextTarget.createMarshaller();
                    marshallerBookdata.marshal(bookdata, arg1.getOutputPayload().getOutputStream());
          catch(Exception e)
          public String convertInputStreamToString(InputStream in) {
                    StringBuffer sb = new StringBuffer();
                    try {
                              InputStreamReader isr = new InputStreamReader(in);
                              Reader reader = new BufferedReader(isr);
                              int ch;
                              while ((ch = > -1) {
                                        sb.append((char) ch);
                    } catch (Exception exception) {
                    return sb.toString();

You would notice that JAXB really does make things easy for one to program. There are methods for nearly everything, making it easy for us to traverse and build XML structures. It also makes the program much more readable. Hope this blog helps you understand another dimension to XML parsing and thus adding another alternative to your traditional Java mapping scenarios.


1. If not using JDK 1.6 and above, you can download the JAR files for JAXB from here

2. We are yet to identify how JAXB based Java mapping performs in comparison to SAX and DOM parser based mapping. This has to be tested out to understand how the mapping will perform under load.

To report this post you need to login first.


You must be Logged on to comment or reply to a post.

  1. Santhosh Kumar Vellingiri

    This options looks interesting and also gives an impression like easy to implement compared to JAXP.

    The Java classes generated for the XSD elements containing set, get, create like methods with the element name itself [eg:createDetailInfo(), detailsS.getTitle()] makes programming much easier and more understandable. These methods looks pretty similar to the java proxy classes generated from wsdl.

    I will personally prefer this, as need not  understand the terms like parentnode, childnode, nodelist, getElementsByTagName, append etc.. 🙂

    This can be an really good alternative for SAX or DOM mapping, if JAXB is identified no worse than SAX or DOM in terms of data processing rate and memory consumption.



Leave a Reply