4

I need to parse XML data from a web service. But, when I am getting the data from the web service the data comes in the format of JSON (In the browser I am seeing the data as XML). So, please guide me how to get xml data into my application.

I am using the following.

In my main activity:

static final String URL = "http://nclex.testshell.net/api/resources";
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Log.e("Response is...",xml);

My xmlParser class

public class XMLParser {

    // constructor
    public XMLParser() {

    }

    /**
     * Getting XML from URL making HTTP request
     * @param url string
     * */
    public String getXmlFromUrl(String url) {
        String xml = null;

        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;
    }
}

output response is

[{"ResourceId":1,"Title":"GRE revised General Test","Description":"The Verbal Reasoning section of the GRE revised General Test","Link":"http://www.ets.org/gre/revised_general/about/content/verbal_reasoning"},{"ResourceId":2,"Title":"GRE Power Pre","Description":"GRE Power Pre","Link":"http://www.number2.com/exams/gre/companion/index.cfm?s=0"},{"ResourceId":3,"Title":"GRE Analytical Writing","Description":"GRE Analytical Writing","Link":"http://www.mygretutor.com/tests.aspx"},{"ResourceId":4,"Title":"GATE Architecture & Planning","Description":"GATE Architecture & Planning","Link":"http://www.onestopgate.com/gate-sample-papers/architecture-planning/"},{"ResourceId":5,"Title":"TarGATE","Description":"GATE to acheive your target","Link":"http://gateforum.com/Testseries-Venue.php"},{"ResourceId":6,"Title":"TOEFL iBT","Description":"TOEFL iBT Test Sample Questions","Link":"https://www.ets.org/toefl/ibt/prepare/sample_questions"}]

3
  • 2
    This is done by web service. Modify your webservice code... Commented Jun 24, 2013 at 6:18
  • Thankyou for ur quick response, then how to change my webservice code Commented Jun 24, 2013 at 6:21
  • view your code of web service. There would be a code that checks incoming response, and look into flags or headers which tells that service "if desktop app requesting or if mobile app requesting". Commented Jun 24, 2013 at 6:24

1 Answer 1

2

The type or format of data you retrieved from the server using web service is totally depends on the language in which web service is implemented and the response code that web service choose to format the data..

Older web services or platforms mainly supports XML format.And the new web services uses the Json format for its light-weight.

In your case,Your web service supports both type of format(XML,Json) and it chooses the format as per the platform from which it receives the request(Mobile,Desktop).(May be it acts as a Generic Webservice).

One way to test the webservice response is to requesting it from platform browser i.e use your platform browser(Desktop or mobile) to see the response for your platform.

Finally,Answer to your question, Change the code at your server side and make sure that it returns XML data as a response for Mobile platform(you don't have to worry about the code at your application side;all changes to be done at server side).

However,you can also convert Json to xml as,

JSONObject Jobj = new JSONObject(jsonString);

Then you can get it in XML format using the XML class, like so:

String xml = org.json.XML.toString(Jobj);

but it's not recommended as it need extra cpu time to convert Json to xml and it can be a problem if your response data is huge.

Sign up to request clarification or add additional context in comments.

Comments

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.