Similar purpose like previous post, this post is using SAX parser in stead.
To understand know more about SAX parser, see this https://www.google.com/?q=sax+parser

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_One2One_SAX extends AbstractTransformation{
    public static void main(String[] args){
        try{
            InputStream input = new FileInputStream(new File("C:\\TEST\\in.xml"));
            OutputStream outout = new FileOutputStream(new File("C:\\TEST\\out.xml"));
            Test_JavaMapping_One2One_SAX mapping = new Test_JavaMapping_One2One_SAX();
            mapping.execute(input, output);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
    public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException{
        InputStream inputstream = transformationInput.getInputPayload().getInputStream();
        OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();
        execute(inputstream, outputstream);
    }
    public void execute(InputStream inputstream, OutputStream outputstream)  {
        try{
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            One2OneHandler handler = new One2OneHandler();
            parser.parse(inputstream, handler);
            String outputString = handler.getOutputString();
            outputstream.write(outputString.getBytes());
        }
        catch (Exception exception){
            exception.printStackTrace();
            this.getTrace().addDebugMessage(exception.toString());
        }
    }
}
class One2OneHandler extends DefaultHandler{
    String sContent = "";
    String sTemp = "";
    StringBuffer fresult = new StringBuffer();
    public String getOutputString(){
        return fresult.toString();
    }
    public void startDocument() throws SAXException{
        fresult.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    }
    public void endDocument() throws SAXException{
    }
    public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException{
        sContent = "";
        sTemp = "<" + name;
        for(int i = 0; i < attributes.getLength(); i++){
            sTemp = sTemp + " " + attributes.getLocalName(i) + "=" + "\"" + attributes.getValue(i) + "\"";
        }
        sTemp = sTemp + ">";
        fresult.append(sTemp);
    }
    public void endElement(String uri, String localName, String name)throws SAXException{
        sTemp = "</" + name + ">";
        fresult.append(sTemp);
    }
    public void characters(char[] ch, int start, int length)throws SAXException{
        sContent = new String(ch, start, length);
        fresult.append(sContent);
    }
}
JAVA Mapping One-to-One Source-to-Target using SAX
Tagged on:     

2 thoughts on “JAVA Mapping One-to-One Source-to-Target using SAX

  • May 27, 2016 at 5:45 pm
    Permalink

    Hi Yee loon,
    what’s the use of transform method that you mentioned in the code?
    I’ve debugged this code using NWDS but don’t see cursor passing through there.
    Thanks in advance 🙂

    Reply
  • May 27, 2016 at 6:20 pm
    Permalink

    I understand that transform is mandatory method to transform input msg to output msg, or you try remove that method, see what error might get?

    Reply

Leave a Reply

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