Skip to content

Instantly share code, notes, and snippets.

@SriniBlog
Last active December 15, 2015 09:39
Show Gist options
  • Save SriniBlog/671dd77124f00d29b32d to your computer and use it in GitHub Desktop.
Save SriniBlog/671dd77124f00d29b32d to your computer and use it in GitHub Desktop.
This sample Java Mapping Shows how to iterate the XML message field by field.
/***** Standard Java Libraries ********/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/***** PI Specific Libraries *********/
import com.sap.aii.mapping.api.AbstractTrace;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
public class JM_IteratePayloadXML extends AbstractTransformation {
BufferedReader docInput;
String inputLine = "";
String messageID = "";
TransformationInput transInput = null;
TransformationOutput transOutput = null;
StringBuffer sourcePayload = new StringBuffer();
StringBuffer destPayload = new StringBuffer();
public static AbstractTrace trace;
public boolean traceon;
public JM_IteratePayloadXML() throws Exception
{
docInput = null;
}
public void transform(TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException
{
trace = (AbstractTrace) getTrace(); //Capture trace object and write trace for debugging purpose.
messageID = arg0.getInputHeader().getMessageId(); //Message ID only available if run from PI.
traceon = true; //make it false if wants to run the program locally. Make it true always before compiling the code and deployment.
if ( traceon ){ trace.addInfo("Java Mapping Program Started!"); }else{ System.out.println("Java Mapping Program Started!"); }
transInput = arg0;
transOutput = arg1;
this.execute((InputStream) arg0.getInputPayload().getInputStream(), (OutputStream) arg1.getOutputPayload().getOutputStream());
if ( traceon ){ trace.addInfo("Java Mapping Program Completed!"); }else{ System.out.println("Java Mapping Program Completed!"); }
}
public void execute(InputStream in, OutputStream out) throws StreamTransformationException {
try {
if ( traceon ){ trace.addInfo("Inside the Execute Method!"); }else{ System.out.println("Inside the Execute Method!"); }
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
DocumentBuilder dBuilder = factory.newDocumentBuilder();
Document doc = dBuilder.parse(in);
doc.getDocumentElement().normalize();
extractChild(doc);
destPayload = destPayload.replace(0, 2, ""); //To remove \r\n first line. if not required then uncomment.
out.write(new String(destPayload).getBytes("UTF-8"));
out.flush(); //flush the stream
if (traceon){ trace.addInfo("Outside the Execute Method!"); }else{ System.out.println("Outside the Execute Method!"); }
} catch (Exception e) {
if( traceon ){ trace.addWarning("[Execute Method]**Error. " + e); }else{ System.out.println("[Execute Method]**Error. " + e); }
}
}
public void extractChild(Node nodes){
if (nodes == null) {
return;
}
int type = nodes.getNodeType();
switch (type) {
case Node.DOCUMENT_NODE: {
extractChild(((Document)nodes).getDocumentElement());
break;
}
case Node.ELEMENT_NODE: {
System.out.print( "\r\n" + nodes.getNodeName() );
destPayload.append( "\r\n" + nodes.getNodeName() );
NodeList childNodes = nodes.getChildNodes();
if( childNodes != null ){
for (int loopIndex = 0; loopIndex < childNodes.getLength(); loopIndex++ ) {
extractChild(childNodes.item(loopIndex));
}
}
break;
}
case Node.TEXT_NODE: {
String value = nodes.getNodeValue().trim();
if( value != null && value.length() > 0 ){
destPayload.append( "=" + value );
System.out.print( "=" + value );
}
break;
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// This method is executed only on the local running and no effect running from PI mapping program
try {
FileInputStream fin = new FileInputStream("inputfile.xml");
FileOutputStream fout = new FileOutputStream("outputfile.txt");
JM_IteratePayloadXML instance = new JM_IteratePayloadXML();
instance.traceon = false; //This will help to print the output instead of trace.
instance.execute(fin, fout);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (StreamTransformationException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/************************************************************************************************************
//BONUS CODE: TO BINARY READ AND WRITE - GOOD WHEN NOTHING TO MASSAGE WITH DATA - FASTEST AS WELL
int buffer;
if ( traceon ){ trace.addInfo("Inside the Execute Method!"); }else{ System.out.println("Inside the Execute Method!"); }
//Read and Write back the file
while ((buffer = in.read()) != -1)
{
out.write(buffer);
}
out.flush();
if (traceon){ trace.addInfo("Outside the Execute Method!"); }else{ System.out.println("Outside the Execute Method!"); }
*************************************************************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment