-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>

1
  • Try googling before asking a question next time Commented Jun 29, 2016 at 10:43

2 Answers 2

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());
    
Sign up to request clarification or add additional context in comments.

Comments

-1

Using foll. code snippet :

JSONObject xmlToJson = XML.toJSONObject(YOUR_XML_STRING);
String jsonString = xmlToJson.toString(ANY_INTEGER_VALUE);

2 Comments

what is XML. in your answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.