我一直在研究一些JSON
并XML
通过 URL 进行解析。我正在使用的 URL 给出JSONObject
了我拥有的XML
.
这是我为此所做的代码:
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>
有人可以帮我在列表视图中修复这些东西吗?帮助将不胜感激。