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"}]