cancel
Showing results for 
Search instead for 
Did you mean: 

JAVA Mapping for XML conversion during runtime

Former Member
0 Kudos

Dear SAP JAVA experts,

For quite I have been struggling to keep the JAVA code in place for JSON to XML conversion being a newbie.

Following is the code snippet.

import java.io.InputStream;

import net.sf.json.JSON;

import net.sf.json.JSONSerializer;

import net.sf.json.xml.XMLSerializer;

import org.apache.commons.io.IOUtils;

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;

import com.sap.aii.utilxi.core.io.IOUtil;

public class RuntimeJSONtoXML extends AbstractTransformation {

  public void transform(TransformationInput input, TransformationOutput output)

  throws StreamTransformationException {

  try

    {

//InputStream is = JSONtoXML.class.getResourceAsStream("JSON.txt");

 

   String strJSON = "";

   InputStream inputStream = input.getInputPayload().getInputStream();

   inputStream.close();

   String jsonData = IOUtils.toString(strJSON);

           XMLSerializer serializer = new XMLSerializer();

           JSON json = JSONSerializer.toJSON( jsonData );

           String xml = serializer.write( json );

 

           //System.out.println(xml);

 

           output.getOutputPayload().getOutputStream().write(strJSON.getBytes());

     }

  catch(Exception ie) { }

  }

  }

Caught unaware of 2 queries.


1.I have added the com.sap.aii.utilxi.core.io.IOUtil jar files from the PI server even though its displays error "The com.sap.aii.utilxi can not be resolved". Also I added the XPI libraries in NWDS but nothing moving to solve the issue.


2. I have commented the line of code where I have placed a test file in the path to test it i.e. JSON.text. But when it is deployed as Archived files, then this code has to be replaced.

The Method toString(InputStream) in the type IOUtils is not applicable for the arguments (String)

Regards

Rebecca..

Accepted Solutions (1)

Accepted Solutions (1)

former_member184720
Active Contributor
0 Kudos

1.I have added the com.sap.aii.utilxi.core.io.IOUtil jar files from the PI server even though its displays error "The com.sap.aii.utilxi can not be resolved". Also I added the XPI libraries in NWDS but nothing moving to solve the issue.

Which jar file did you add?

/usr/sap/<<SID>>/DVEBMGS<<SYSNO>>/j2ee/cluster/bin/ext/com.sap.xi.util.misc/lib

jar file name : com.sap.aii.utilxi.core.jar

----------------------------------------

2. I have commented the line of code where I have placed a test file in the path to test it i.e. JSON.text. But when it is deployed as Archived files, then this code has to be replaced.

The Method toString(InputStream) in the type IOUtils is not applicable for the arguments (String)



To read the input stream you should have


InputStream inputstream = transformationInput.getInputPayload().getInputStream();

Please refer to below blog just to get an idea on working with input stream and output stream.

Dynamic file name for pass-through scenario - Process Integration - SCN Wiki

Former Member
0 Kudos

Dear Hareesh,

Thanks for your valuable input...

The above code I would be using for the Rest Webservices response where the JAVA application would respond back in JSON format by a application/xx-url-encoded request. Could you please help me on this.

1. I am not sure whether the above code would work in such scenario.Not sure whether the response will be any file or not. Would the above code works if the response is in the form of strings is passed to JAVA mapping

{"items": [

    {

  "item":

     {

  "WFID": "79C675F718122E4792F4E52491945E1A",

  "Subject": " by Rebecca",

  "DocumentID": "555E476D",

  "Is": "1",

  "Count": "7"

  }

  }

]

}

2. Could you suggest me what piece of code I have to add in JAVA mapping so that the response received from REST web service can be converted into XML.

Regards

former_member184720
Active Contributor
0 Kudos

change the extension to jar and import

your json returned the below xml for me..

<items>

  <item>

  <DocumentID>555E476D</DocumentID>

  <Count>7</Count>

  <Subject> by Rebecca</Subject>

  <WFID>79C675F718122E4792F4E52491945E1A</WFID>

  <Is>1</Is>

  </item>

</items>

Former Member
0 Kudos

Dear Hareesh,

Sorry to bother you..

Could you please share the code which you compiled....

Regards

Rebecca

former_member184720
Active Contributor
0 Kudos

I've already attached it in my previous reply. Didn't you find it in the zip file?

Former Member
0 Kudos

Dear Hareesh,

Thanks for your help..

The file after download is getting converted into junk data and I am able to see most of the characters as junk. Only the copy right description I can see and the rest in as junk. Tried with Notepad and Notepad++.The efforts are in vain.

Request you to please share JSON to XML in a word doc.

Regards...

former_member184720
Active Contributor
0 Kudos

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import org.json.JSONObject;

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 xml2json extends AbstractTransformation {

public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException {

    try {

  String sourcexml = ""; String targetxml =""; String line ="";

  InputStream ins =    in.getInputPayload().getInputStream();

  BufferedReader br = new BufferedReader( new InputStreamReader(ins));

  while ((line = br.readLine()) != null) 

  sourcexml +=line+"\n"; 

  br.close();

  JSONObject o = new JSONObject(sourcexml);

  targetxml = org.json.XML.toString(o);

  out.getOutputPayload().getOutputStream().write(targetxml.getBytes());

   

  }

    catch (Exception e) {

           throw new StreamTransformationException(e.getMessage());

  }

}

}

You need to download the "java-json.jar" and add it buildpath

Download java-json.jar : java json&amp;nbsp;&amp;laquo;&amp;nbsp;j&amp;nbsp;&amp;laquo;&amp;nbsp;Ja...

Former Member
0 Kudos

Dear Hereesh,

I found some errors when testing with multiple records. The sample input file we tried was with single item record but  when more than 1  record it has error.


Error when parsing an XML document (The markup in the document following the root element must be well-formed.)    

The sample JSON input file is

{

    "items": [

        {

            "item": {

                "Subject": "test wared by wael",

                "DocumentID": "55DCE0A476D",

                "Sender": "Rebecca",

                "SenderDomainName": "user1",

                "Date": "2013-02-14 11:14:40",

                "DocumentClassID": "11",

                "Comment": "",

                "DaysCount": "3"

            }

        },

        {

            "item": {

                "Subject": "test wared by wael",

                "DocumentID": "55DCE191D5E47",

                "Sender": "Dave Froster",

                "SenderDomainName": "user11",

                "Date": "2013-03-14 11:14:40",

                "DocumentClassID": "11",

                "Comment": ""

            }

        }

    ]

}

Please guide.

Regards..........

Answers (0)