cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Java Mapping - Convert Xml To Json - With Array

mlmendes
Participant
0 Likes
5,844

Hi Gurus!!!

I'm having problems with describe a java code to convert Xml to Json.

i had saw any codes in blogs, but all this codes has problem with Array.

this is my goal:

but my java code is generating this Json:

Someone, can help me with any idea how can I create this code correctly? I'll Appreciate it!!

View Entire Topic
sugata_bagchi2
Active Contributor
0 Likes

you can use the external jar JSONObject.jar. I have given a sample code which is taking two test nodes under a root node and converting it into JSON Array -

package com.sap.XML2JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;

public class XML2JSONArray
{

     public static int PRETTY_PRINT_INDENT_FACTOR = 4;
        public static String TEST_XML_STRING =
            "<?xml version=\"1.0\" ?><root><test attrib=\"jsontext\">abctest</test><test attrib=\"jsontext2\">deftest</test></root>";

        public static void main(String[] args) {
            try {
                JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
                String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
                System.out.println(jsonPrettyPrintString);
            } catch (JSONException je) {
                System.out.println(je.toString());
            }
        }
    }

    

and here is the output - 
{"root": {"test": [
    {
        "attrib": "jsontext",
        "content": "abctest"
    },
    {
        "attrib": "jsontext2",
        "content": "deftest"
    }
]}}

mlmendes
Participant
0 Likes

Hi Sugata!!

I've the same code that you create.

package Xml2Json;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.json.JSONObject;
import org.json.XML;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

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 MakeItJSON extends AbstractTransformation
{
public static int PRETTY_PRINT_INDENT_FACTOR = 4;

public MakeItJSON() {}

public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException
{
try {
String sourcexml = "";
String targetfile = "";
String line = "";



InputStream ins = in.getInputPayload().getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(ins));

while ((line = br.readLine()) != null) {
sourcexml = sourcexml + line;
}


JSONObject jsonObject = XML.toJSONObject(sourcexml);
String jsonPrettyPrintString = jsonObject.toString(PRETTY_PRINT_INDENT_FACTOR);


targetfile = jsonPrettyPrintString;

out.getOutputPayload().getOutputStream().write(targetfile.getBytes());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

but if i put more "items" the json code not create all the items.