2

How to convert XML to JSON in java servlet.

    <?xml><SOAP-ENV:Envelope xmlns:xsd= "http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:HNS="http://tempuri.org/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header><HNS:ROClientID SOAP-ENV:mustUnderstand="0">{6C9A8E69-2018-4090-8FA7-DEB98300E102}</HNS:ROClientID></SOAP-ENV:Header><SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ro="http://tempuri.org/"><NS1:GetStationListResponse xmlns:NS1="urn:WOOSServices-WOrbitService"><Stations xsi:type="xsd:string"></Stations><Result xsi:type="xsd:string">{
    "MOColmns": [
        {
            "MOTitle": "Description"
        },
        {
            "MOTitle": "station_name"
        },
        {
            "MOTitle": "StationID"
        },
        {
            "MOTitle": "StationINT"
        }
    ]
}</Result></NS1:GetStationListResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>";
            String xml = "<xx yy='nn'><mm>zzz</mm></xx>";

            JSONArray json = (JSONArray) XMLSerializer.read(xml);  
            System.out.println( json ); 

please help me.

1
  • 2
    Any reason why the answer is not accepted? :-) Commented Jan 29, 2013 at 21:00

4 Answers 4

10

You can grab a set of Java classes to handle JSON at http://json.org/java/

There you can find the XML and JSONObject classes, among others. This code could work for you:

public String XMLtoJSON(String xml) {
    JSONObject jsonObj = XML.toJSONObject(xml);
    String json = jsonObj.toString();
    return json;
}
Sign up to request clarification or add additional context in comments.

4 Comments

In @antur123's answer, I believe all instances of "string" should be "String", in which case that code should work.
@selladurai Any reason this is not selected as the answer?
Excellent observation, @RajeevM, :-)
Hi antur123, I can't able to handle XML.toJSONObject(xml) because i can able to resolve XML, how can i resolve this? please help me.
0

You need to import org.json.XML in your class to resolve XML. Or Put org.json.XML to your classpath.

Comments

0

Underscore-java can convert xml to json.

import com.github.underscore.U;

String xml = "<xx yy=\"nn\"><mm>zzz</mm></xx>";

String json = U.xmlToJson(xml);  
System.out.println(json);

Comments

0

I know this is an old thread. Just wanted to share that I have come up with a generic Java-based XML-to-JSON converter which is able to convert any given XML to a desired JSON. Here is the link: https://github.com/mohapatra-sambit/xml-to-json-converter

Hope this helps!!!

1 Comment

Maybe you could edit your answer and post code that shows how to use your converter to convert the sample XML in the posted question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.