我正在开发一个应用程序,其中我从URL获得JSON
响应。在JSON
响应中,我必须首先解析JSONObject
。然后,我必须解析JSONObject
中的XML内容。以下是我收到的JSON
响应示例:
{
"output": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Results>\n <Feed prov=\"dmoz\">\n <ResultSet id=\"webListings\" source=\"DMOZ\">\n <Listing description=\" - A bike shop in Brisbane. Stocks mountain bikes, road bikes, and BMX bikes.\n \" rank=\"1\" siteHost=\"http://www.lifecycle.net.au/\" title=\"Lifecycle Bike Shop\">\n <ClickUrl type=\"body\">http://www.lifecycle.net.au/</ClickUrl>\n </Listing>\n <Listing description=\" - Videos and pictures taken of both sport bikes and dirt bikes.\n \" rank=\"2\" siteHost=\"http://roadanddirt.com/\" title=\"Road and Dirt\">\n <ClickUrl type=\"body\">http://roadanddirt.com/</ClickUrl>\n </Listing>\n</Results>"
}
以下是我的Java代码,用于解析API并在XML
中获取String
:
HttpClient hClient = new DefaultHttpClient();
HttpGet hGet = new HttpGet("API here");
ResponseHandler<String> rHandler = new BasicResponseHandler();
data = hClient.execute(hGet, rHandler);
JSONObject json = new JSONObject(data);
// get xml string form jsonObject
String str_xml = json.getString("output");
我已经解析了JSONObject
并且现在在String
中获取整个XML,现在我必须解析整个XML并在列表视图中修改这些XML。请帮我解析XML
并将其修复到listview中。将不胜感激。
答案 0 :(得分:1)
从当前json获取xml字符串:
JSONObject json = new JSONObject("your json response");
// get xml string form jsonObject
String str_xml=json.getString("output");
// now convert str_xml to xml document for xml parsing
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
InputSource inStream = new InputSource();
inStream.setCharacterStream(new StringReader(str_xml));
Document doc = db.parse(inStream); //<<< get xml Document here