In this example, demonstrated Java mapping to concatenate first name and last name to full name using SAX.
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 org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.sap.aii.mapping.api.*;
public class Test_JavaMapping_FirstNameLastName2FullName_SAX extends AbstractTransformation {
    public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException {
        try {
            InputStream inputstream = transformationInput.getInputPayload().getInputStream();
            OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            FullNameHandler handler = new FullNameHandler();
            parser.parse(inputstream, handler);
            String outputString = handler.getOutputString();
            outputstream.write(outputString.getBytes());
        } catch (Exception exception) {
            throw new StreamTransformationException(exception.toString());
        }
    }
}
class FullNameHandler extends DefaultHandler {
    String sContent = "";
    String sName = "";
    String sTemp = "";
    StringBuffer fresult = new StringBuffer();
    String sFirstName = "";
    String sLastName = "";
    public String getOutputString(){
        return fresult.toString();
    }
    public void startDocument() throws SAXException {
        fresult.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        fresult.append("<MT_FULLNAME>");
    }
    public void endDocument() throws SAXException {
        fresult.append("</MT_FULLNAME>");
    }
    public void startElement(String uri, String localName, String name, Attributes attributes)throws SAXException{
        sContent = "";
        sName = name;
        if("row".equalsIgnoreCase(name)){
            sFirstName = "";
            sLastName = "";
        }
    }
    public void endElement(String uri, String localName, String name) throws SAXException{
        if("FirstName".equalsIgnoreCase(name)){
            sFirstName = sContent;
        }
        if("LastName".equalsIgnoreCase(name)){
            sLastName = sContent;
        }
        if("row".equalsIgnoreCase(name)){
            fresult.append("<row>");
            fresult.append("<FullName>");
            fresult.append(sFirstName + " " + sLastName);
            fresult.append("</FullName>");
            fresult.append("</row>");
        }
    }
    public void characters(char[] ch, int start, int length)throws SAXException{
        sContent = new String(ch, start, length);
    }
}
JAVA Mapping Simple FirstName LastName to FullName using SAX
Tagged on:     

2 thoughts on “JAVA Mapping Simple FirstName LastName to FullName using SAX

  • June 10, 2017 at 6:46 pm
    Permalink

    Hey Yee Loon,
    Can we able to test it in NWDS directly before importing into ESR?

    Reply
  • June 10, 2017 at 6:47 pm
    Permalink

    Hey Yee Loon,
    Can we able to test it in NWDS directlly before importing into ESR?

    Reply

Leave a Reply

Your email address will not be published.