In this example, demonstrated Java mapping to concatenate first name and last name to full name using DOM.
Source XML:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_FIRSTNAME_LASTNAME xmlns:ns0="http://test.com/common">
   <row>
      <FirstName>Yun Fat</FirstName>
      <LastName>Chow</LastName>
   </row>
   <row>
      <FirstName>Michael</FirstName>
      <LastName>Jackson</LastName>
   </row>
</ns0:MT_FIRSTNAME_LASTNAME>

Target XML:

 <?xml version="1.0" encoding="UTF-8"?>
<MT_FULLNAME>
   <row>
      <FullName>Yun Fat Chow</FullName>
   </row>
   <row>
      <FullName>Michael Jackson</FullName>
   </row>
</MT_FULLNAME>

JAVA mapping class:

package com.map;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import com.sap.aii.mapping.api.*;
public class Test_JavaMapping_FirstNameLastName2FullName_DOM extends AbstractTransformation {
    public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException {
        try {
            InputStream inputstream = transformationInput.getInputPayload().getInputStream();
            OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();
            DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document inDoc = docBuilder.parse(inputstream);
            Document outDoc = docBuilder.newDocument();
            Element oMT = outDoc.createElement("MT_FULLNAME");
            outDoc.appendChild(oMT);
            NodeList nodes = inDoc.getElementsByTagName("row");
            for (int i = 0; i < nodes.getLength(); i++){
                Element oRow = outDoc.createElement("row");
                oMT.appendChild(oRow);
                Node node = nodes.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE){
                    Element element = (Element)node;
                    String sFirstName = element.getElementsByTagName("FirstName").item(0).getTextContent();
                    String sLastName = element.getElementsByTagName("LastName").item(0).getTextContent();
                    String sFullName = sFirstName + " " + sLastName;
                    Element oFullName = outDoc.createElement("FullName");
                    oFullName.appendChild(outDoc.createTextNode(sFullName));
                    oRow.appendChild(oFullName);
                 }
            }
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.transform(new DOMSource(outDoc), new StreamResult(outputstream));
        } catch (Exception exception) {
            throw new StreamTransformationException(exception.toString());
        }
    }
}
JAVA Mapping Simple FirstName LastName to FullName using DOM
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *