zDroid
zDroid

Reputation: 426

Simple json to xml conversion

can anyone share code to convert json to xml this is the json that comes through the request

{'sector':'Europe','organization':'Bazz Market Unit UK','companyCode':'UK1_2020','approvalManager':'03000069','managementLevel1':'X11','approvalLimit':'500000','access Category':'FTeam','currency':'USD','comments':'Need this access so I can do my job properly'}

I need json to xml as well as vice- versa .Can any one help me out, I'd prefer code with no jar imports required.

Thanks in advance

Upvotes: 0

Views: 2634

Answers (3)

Valentyn Kolesnikov
Valentyn Kolesnikov

Reputation: 2097

Underscore-java library has static method U.jsonToXml(json).

Upvotes: 0

zDroid
zDroid

Reputation: 426

I tried this and works pretty well for me...

json to xml -

JSON jsonObject = JSONSerializer.toJSON(json); 
    XMLSerializer xmlSerialized = (new XMLSerializer());
    xmlSerialized.setTypeHintsEnabled(false);
    String xml = xmlSerialized.write( jsonObject );

and xml to json

org.json.JSONObject object;
    String json = null;
    try {
        object = XML.toJSONObject(xml);
         json = object.toString();
    } catch (JSONException e) {
        e.printStackTrace();
    }

Hope this helps.. :)

Upvotes: 1

helderdarocha
helderdarocha

Reputation: 23637

If you are using Java SE and can't use foreign JARs, and your JSON is always simple as the example you posted, you can parse it. Here's a short program that works for your example (but you will certainly have to adapt it if you have more complex JSON strings with more nesting levels and arrays):

public class SimpleJsonToXml {
    public static void main(String[] args) {
        String json = "{'sector':'Europe','organization':'Bazz Market Unit UK','companyCode':'UK1_2020','approvalManager':'03000069','managementLevel1':'X11','approvalLimit':'500000','access Category':'FTeam','currency':'USD','comments':'Need this access, so I can do my job properly'}";
        String jsonObject = json.replaceAll("\\{(.*)\\}", "$1");
        String[] childElements = jsonObject.split("(,)(?=(?:[^\']|\'[^\']*\')*$)");

        System.out.println("<root>");
        for (String item : childElements) {
            System.out.println(makeTag(item));
        }
        System.out.println("</root>");
    }

    public static String makeTag(String jsonProperty) {
        String[] element = jsonProperty.split("\\:");
        String tagName = element[0].replaceAll("[' ]", "");
        String tagValue = element[1].replace("'", "");
        return "    <"+tagName+">"+tagValue+"</"+tagName+">";
    }
}

It will print:

<root>
    <sector>Europe</sector>
    <organization>Bazz Market Unit UK</organization>
    <companyCode>UK1_2020</companyCode>
    <approvalManager>03000069</approvalManager>
    <managementLevel1>X11</managementLevel1>
    <approvalLimit>500000</approvalLimit>
    <accessCategory>FTeam</accessCategory>
    <currency>USD</currency>
    <comments>Need this access, so I can do my job properly</comments>
</root>

To convert XML back to JSON you can use the native Java XML tools and parsers (org.w3c.dom and org.xml.sax, for example) and won't need any external Jars.

If you are using at least Java EE 7, you can use the parsers in the javax.json package.

Upvotes: 1

Related Questions