我无法从RSS数据...空例外 我从这个link检索,它只有如何从rss获取数据?
<?xml version="1.0" encoding="utf-8"?>
<GoldQuotes>
<Price Date="2013-11-28 09:22" Value="1244.30" />
<Price Date="2013-11-28 09:20" Value="1243.10"/>
<Price Date="2013-11-28 09:18" Value="1243.30"/>
[...]
</GoldQuotes>
这是我的Java代码片断
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(in);
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getElementsByTagName("GoldQuotes"); //***
if ((nl != null) && (nl.getLength() > 0)) {
for (int i = 0; i < nl.getLength(); i++) {
dissectNode(nl, i);
}
}//if
[...]
public void dissectNode(NodeList nl, int i) {
try {
Element entry = (Element) nl.item(i);
Element price = (Element) entry.getElementsByTagName("Price").item(0);
String priceValue = price.getAttribute("Value"); //get gold value
SingleNewsItem singleItem = new SingleNewsItem(priceValue);
newsList.add(singleItem);
} catch (DOMException e) {
e.printStackTrace();
}
}// dissectNode
我做NodeList nl = docEle.getElementsByTagName("GoldQuotes")
后。我与nl.getLength()
测试,其中返回0 .. 我错过了什么?
'GoldQuotes'是文档根元素,通过'getDocumentElement()'返回;在id里面没有嵌套的GoldQuotes元素,所以'docEle.getElementsByTagName(“GoldQuotes”)'正确地返回一个空的列表 –
所以我想我该怎么办? –
lynndragon