我需要从 Web 服务解析 XML 数据。但是,当我从 Web 服务获取数据时,数据采用 JSON 格式(在浏览器中,我将数据视为 XML)。所以,请指导我如何将 xml 数据导入我的应用程序。
我正在使用以下内容。
在我的主要活动中:
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);
我的 xmlParser 类
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;
}
}
输出响应是
[{"ResourceId":1,"Title":"GRE 修改后的普通考试","Description":"GRE 修改后的普通考试的语言推理部分","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 架构 &规划","描述":"GATE 架构与规划","链接":" http://www.onestopgate.com/gate-sample-papers/architecture-planning/"},{"ResourceId":5,"Title":"TarGATE","Description":"GATE 实现你的目标","Link":" http://gateforum.com/Testseries-Venue.php "} ,{"ResourceId":6,"Title":"TOEFL iBT","Description":"TOEFL iBT 考试样题","Link":" https://www.ets.org/toefl/ibt/prepare/示例问题“}]