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;
In the next part, we will look at how to code and use JAXB to our advantage.