Skip to Content

Dear All,

Here I have tried a small POC to handle CDATA input and create a target payload using Java Mapping.

Input structure:

/wp-content/uploads/2014/09/1_547490.jpg

Target Structure:

/wp-content/uploads/2014/09/2_547491.jpg

Input payload:

<?xml version=”1.0″ encoding=”UTF-8″?>

<ns0:Source_DT1 xmlns:ns0=”urn:NGM:POC”>

   <Root>

      <Field1><![CDATA[<?xml version=”1.0″ encoding=”UTF-8″?> <ns0:Source_DT1 xmlns:ns0=”urn:NGM:POC”> <Root>       <A>A</A>       <B>B</B>                 <C>B</C>    </Root> </ns0:Source_DT1>]]></Field1>

   </Root>

</ns0:Source_DT1>

/wp-content/uploads/2014/09/3_547492.jpg

Mapping Explanation:

In input value Field1, We are getting XML contains values for A, B and C. So we have to get the values for Target Fields A, B and C from input Field1 XML value and create target.

Java Mapping:

This is handled through Below Java mapping:

We are creating target structure from the field1 CDATA value.

/wp-content/uploads/2014/09/4_547493.jpg

Here is the java mapping which I had used to create target payload:


public void transform(TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException {

String inputPayload = convertInputStreamToString(arg0.getInputPayload().getInputStream()); // Calls convertInputStreamToString function

String output = “”;

String outputPayload = “”;

output=inputPayload.replaceAll(“<.*?>”, “”);//Regx to remove everything between “<>” characters

output=output.replaceAll(“]]>”, “”);//replace the charcters with empty

output=output.replaceAll(“\\s+”, ” “);//Regx to remove the extra spaces

try {

StringTokenizer st = new StringTokenizer(output, ” ” );

        String[] strFileArray = new String[4];

        int i = 0;

        while (st.hasMoreTokens()) {

strFileArray[i] = st.nextToken();

            i++;

        }

//————-Creating target structure————//

outputPayload = outputPayload .concat(“<?xml version=\”1.0\” encoding=\”UTF-8\”?>”).concat(“\r\n”);

outputPayload = outputPayload .concat(“<Target_DT1>”).concat(“\r\n”);

outputPayload = outputPayload .concat(“<Root>”).concat(“\r\n”);

outputPayload = outputPayload .concat(“<A>”).concat(strFileArray[0]).concat(“</A>”).concat(“\r\n”);

outputPayload = outputPayload .concat(“<B>”).concat(strFileArray[1]).concat(“</B>”).concat(“\r\n”);

outputPayload = outputPayload .concat(“<C>”).concat(strFileArray[2]).concat(“</C>”).concat(“\r\n”);

outputPayload = outputPayload .concat(“</Root>”).concat(“\r\n”);

outputPayload = outputPayload .concat(“</Target_DT1>”).concat(“\r\n”);

/*

* Output payload is returned using the TransformationOutput class             

* arg1.getOutputPayload().getOutputStream(

*/

arg1.getOutputPayload().getOutputStream().write(outputPayload.getBytes(“UTF-8”));

} catch (Exception exception1) {

getTrace().addWarning(“Exception caught in Transform: ” + exception1.toString());

}

}

/* <<< Reads input payload>>>*/

public String convertInputStreamToString(InputStream in) {

StringBuffer sb = new StringBuffer();

try {

InputStreamReader isr = new InputStreamReader(in);

Reader reader = new BufferedReader(isr);

int ch;

while ((ch = in.read()) > -1) {

  1. sb.append((char) ch);

}

  1. reader.close();

} catch (Exception exception) {

getTrace().addWarning(“Exception caught in convertInputStreamToString: “+ exception.toString());

}

return sb.toString();

}

Final Output:

Fianl target payload and sturucture will looks as below.

Target Payload:

/wp-content/uploads/2014/09/5_547494.jpg

Target Structure:

/wp-content/uploads/2014/09/6_547495.jpg

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply