Skip to Content

Traditionally when PI consultants are asked about Java mapping, SAX and DOM parsers would be the first to cross their minds. But this blog is here to change that view.

After the blog XML / XSD Validation – An Alternative for SAP PI, I happened to receive queries around JAXB and its usage. Since JAXB is a XML binding framework, one of the best use cases it has in the world of integration is around XML mapping.

In this blog we will look at how to use JAXB and thus develop a Java mapping without the use of a SAX or DOM parser.

Below is a quick reference to JAXB, as it will not be in the scope of this blog to explain JAXB in detail;

1) Java Architecture for XML Binding (JAXB) – http://www.oracle.com/technetwork/articles/javase/index-140168.html

2) Marshal and Unmarshal in JAXB – http://docs.oracle.com/javaee/5/tutorial/doc/bnbay.html

Setting up the Environment:

1) Source XSD

Below is the schema that we will use for the source message;

2) Target XSD

Below is the graphical schema representation of the target message;

3. Create a java project in NWDS. Create two folders, sourceData and targetData with packages sourceData and targetData respectively.

4. Import the source XSD into the sourceData folder and the Target XSD into the targetData folder.

5. Using XJC to create the required java classes related to the source and target XSD

There are two ways you can use XJC to build the Java classes for your XSDs.

a. Use the command line, XJC <XSD File name> . If you want to have a control on the package name created for these classes use, XJC –p <package name> <XSD File name>

Once the files are generated, import them into their respective folders i.e sourceData or targetData

b. External tool in NWDS – Configure the XJC bat file as an external tool in NWDS and use it to generate the classes directly in NWDS itself.

Your NWDS should look something as below;

JAXB_12June2012_1.JPG

In the next part, we will look at how to code and use JAXB to our advantage.

To report this post you need to login first.

1 Comment

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

  1. Otto Frost

    Instead of using the xjc for generation, you can use the SAP graphical wizards. 

    In ESR perspective, click the service interface

    Generate Client

    follow the wizard to generate the artifacts.

    sometimes it is necessary to mess with the classloader, otherwise the marshalling unmarshalling won’t work or it is impossinle to create jaxb context

     

    ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
      try {
       Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
    // JAXB coding and mapping

    String contextPath = ItemType.class.getPackage().getName();

    JAXBContext jc = JAXBContext.newInstance(contextPath);

    // more coding

      } finally {
       Thread.currentThread().setContextClassLoader(oldLoader);
      }

    using the wizard avoids using jaxb customizing which can be tricky

    (0) 

Leave a Reply