【发布时间】:2013-12-14 05:45:09
【问题描述】:
我无法从 rss 获取数据...空异常 我从这个link 中检索,它只有
<?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")正确返回一个空列表 -
所以我想要
我该怎么做?
标签: java android xml xml-parsing rss