我一直在努力通过URL解析一些JSON
和XML
。我使用的网址为JSONObject
,其中我有XML
。Android:XML通过URL解析
这里是我的代码是我一直在做这个:
HttpClient hClient = new DefaultHttpClient();
HttpGet hGet = new HttpGet(
"URL for getting results");
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");
// 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); // <<< getting xml Document here
这里是JSONObject
从中我得到XML
:
{
"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
和修复他们在列表视图。下面是示例XML
这我越来越:
<Results>
<Feed prov="dmoz" >
<ResultSet id="webListings" source="DMOZ" >
<Listing description=" - A bike shop in Brisbane. Stocks mountain bikes, road bikes, and BMX bikes. " rank="1" siteHost="http://www.lifecycle.net.au/" title="Lifecycle Bike Shop" >
<ClickUrl type="body" >http://www.lifecycle.net.au/</ClickUrl>
</Listing>
<Listing description=" - Videos and pictures taken of both sport bikes and dirt bikes. " rank="2" siteHost="http://roadanddirt.com/" title="Road and Dirt" >
<ClickUrl type="body" >http://roadanddirt.com/</ClickUrl>
</Listing>
<Listing description=" - Resource dedicated to pocket bike enthusiasts both in Australia and overseas. " rank="4" siteHost="http://www.pocket-bike-racing.com.au/" title="Pocket Bike Racing" >
<ClickUrl type="body" >http://www.pocket-bike-racing.com.au/</ClickUrl>
</Listing>
</ResultSet>
</Feed>
</Results>
有人能帮助我在列表视图修复这些东西。帮助将不胜感激。
创建一个列表视图,并使用列表视图适配器将解析的XML内容插入到ListView中。 – 2013-02-18 06:46:30
我的查询是如何解析XML,因为我得到了'doc' varibale中的整个XML,如代码所示。你对此有任何想法吗? – Anupam 2013-02-18 06:48:13
您是否需要将此xml从JSON对象中取出的帮助? – 2013-02-18 06:51:34