Skip to end of metadata
Go to start of metadata

Frequent question on SDN;

1. How can I map my incoming flat file into a single field of an XML?

2. How can I get the complete payload of a message into a single node/field of an XML?

So this is how it can be done.

 If using PI 7.1 and above, you can use this simple option as mentioned in the below blog;

 http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/19719

Else if you are on a version below 7.1; 

Option : JAVA Mapping

 A very simple java code can help you achieve the results. Note that this is not a UDF but a java mapping.

import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.AbstractTrace;
import com.sap.aii.mapping.api.StreamTransformationConstants;

import java.util.Map;
import java.io.*;

public class PayloadToXMLField implements StreamTransformation {
    String strXML = new String();

	//Declare the XML tag for your XML message
	String StartXMLTag = "<Payload>";
	String EndXMLTag = "</Payload>";


    AbstractTrace trace;
    private Map param = null;

    public void setParameter(Map param) {
        this.param = param;

    }

    public void execute(InputStream in, OutputStream out) {

        trace =
            (AbstractTrace) param.get(
                StreamTransformationConstants.MAPPING_TRACE);
        trace.addInfo("Process Started");


        try {
            StringBuffer strbuffer = new StringBuffer();
            byte[] b = new byte[4096];
            for (int n;(n = in.read(b)) != -1;) {
                strbuffer.append(new String(b, 0, n));
            }
            strXML = strbuffer.toString();
        } catch (Exception e) {
            System.out.println("Exception Occurred");
        }

        outputPayload =
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
			+ StartXMLTag
			+ strXML
			+ EndXMLTag;

        try {
            out.write(outputPayload.getBytes());
			trace.addInfo("Process Completed");;
        } catch (Exception e) {
            trace.addInfo("Process Terminated: Error in writing out payload");;
        }
    }
}

 
Explanation:

The code above is extremely simple. All it does is the follows;

1. Converts the inputstream that will contain the source payload (it can be a flat file or XML etc) to string

2. An output string is created using string concat. Assign tags and place the output string which has your complete source between it.

3. Write out the result to an outputstream.

Note:

1. In FILE adapter, do not use any FCC parameters. Give the Message protocol as FILE. This is in case you want the whole content of the file to come into XI

2. In java mapping, you will have to use the jar aii_map_api.jar

To locate the jar, Go to your XI server folders and use this path, j2ee\cluster\server0\apps\sap.com\com.sap.xi.services. Inside com.sap.xi.services folder you'll find the jar file. Import the jar for your mapping.

1 Comment

  1. Unknown User (f4cqw7o)

    hi sabharish,

    i got a linkage when i was trying to implement the java mapping mentioned in this wiki.

    could you please suggest any modification. i was using java 1.6 in my machine earlier and then i tried with java 1.5 installed in other machine also but the error didnt resolved.