0

我正在开发一个应用程序,在该应用程序中我JSON从一个 URL 获得响应。在那个JSON响应中,我必须先解析JSONObject。然后,我必须解析 .xml 文件中的 XML 内容JSONObjectJSON这是我得到的响应示例:

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

XML这是我用于解析 API 并进入的Java 代码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");

我已经解析了JSONObjecta 中String的整个 XML,现在我必须解析整个 XML 并修复列表视图中的那些,明智的分类。请帮助我解析XML并将它们修复到列表视图中。帮助将不胜感激。

4

1 回答 1

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
于 2013-02-07T09:58:34.660 回答