-2

I have an app in which I have to send String request to server and server is sending me response in xml format.How can I read this response ans save it in String variable and convert into json.

server is reponding reponse like:-

<Response>
<st-code>0</st-code>
<st-desc>Transaction Successful</st-desc>
<st-no>XXXXXX</st-no>
<st-tno>111111</st-tno>
<st-optno>XXXXXXXXX</st-optno>

Siddharth
  • 181
  • 2
  • 11

2 Answers2

0
  • Download the following library java-json
  • Add this library to /libs of your project
  • Now Import the required libraries
    import org.json.JSONException;
    import org.json.JSONObject;
    import org.json.XML;
  • Now use the following code to convert your xml string to json

    sampleXml = "";//XML string to convert it to JSON
    JSONObject jsonObj = null;
    try 
    {
        jsonObj = XML.toJSONObject(sampleXml);
    } 
    catch (JSONException e) 
    {
        Log.e("JSON exception", e.getMessage());
        e.printStackTrace();
    } 
    
    Log.d("XML", sampleXml);
    
    Log.d("JSON", jsonObj.toString());
    
Sunny Mane
  • 121
  • 1
  • 10
-1

Using foll. code snippet :

JSONObject xmlToJson = XML.toJSONObject(YOUR_XML_STRING);
String jsonString = xmlToJson.toString(ANY_INTEGER_VALUE);
Onkar Nene
  • 1,359
  • 1
  • 17
  • 23