Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

JAVA Mapping for XML conversion during runtime

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..

Former Member
replied

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 « j « Jar File Download

0 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question