Quantcast
Channel: SCN : Unanswered Discussions - Process Integration (PI) & SOA Middleware
Viewing all articles
Browse latest Browse all 6719

JAVA mapping for well formed XML

$
0
0

Dear Experts,

 

I am trying to convert JSON data to XML with the below code which was shared by Hareesh. It works

fine with single item record but when multiple items are there , it doesn't form well formed XML structure.It gives multiple root element.

 

                                                            Code

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());

  }

}

}

 

 

                                                                               Payload

 

{

    "items": [

        {

            "item": {

                "Subject": "test wared by wael",

                "DocumentID": "55DCE0A476D",

                "Sender": "Rebecca",

                "SenderDomainName": "user1",

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

                "DocumentClassID": "11"

            }

        },

        {

            "item": {

                "Subject": "test wared by wael",

                "DocumentID": "55DCE191D5E47",

                "Sender": "Dave Froster",

                "SenderDomainName": "user11",

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

                "DocumentClassID": "11"

            }

        }

    ]

}

 

 

What line of code can be added so that the XML result comes with XML declaration and with 1 root node.

 

 

Regards..


Viewing all articles
Browse latest Browse all 6719

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>