To get started to develop JAVA mapping in SAP NWDS, go to this excellent post by Raghu
Below PI java mapping parse source xml file into DOM, then output the same DOM to target xml. Very basic just to show and give a feeling the required coding to achieve this requirement.

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.*;
import com.sap.aii.mapping.api.*;
public class Test_JavaMapping_One2One_DOM extends AbstractTransformation {
    //This main function is for unit test debug only. Not required by SAP PI/PO
    public static void main(String[] args){
        try{
            InputStream input = new FileInputStream(new File("C:\\TEST\\in.xml"));
            OutputStream output = new FileOutputStream(new File("C:\\TEST\\out.xml"));
            Test_JavaMapping_One2One_DOM mapping = new Test_JavaMapping_One2One_DOM();
            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{
            DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document inDoc = docBuilder.parse(inputstream);
            Document outDoc = docBuilder.newDocument();
            //here just assign inDoc to outDoc. If want to have different outDoc, loop inDoc and manipulate outDoc as needed.
            outDoc = inDoc;
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.transform(new DOMSource(outDoc), new StreamResult(outputstream));
        }catch (Exception e){
            e.printStackTrace();
            this.getTrace().addDebugMessage(e.toString());
        }
    }
}
JAVA Mapping One-to-One Source-to-Target using DOM
Tagged on:     

One thought on “JAVA Mapping One-to-One Source-to-Target using DOM

Leave a Reply

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