一切都好。从互联网获取 xml,解析为 Document 但是当我试图从节点()和一个节点之间获取一些值时(非常多云。)
private void parseWeather(Document srcDoc) throws Exception {
for (int a = 0; a < srcDoc.getElementsByTagName("record").getLength(); a++){
Node recordNode = srcDoc.getElementsByTagName("record").item(a);
Node temperatureNode = srcDoc.getElementsByTagName("temperature").item(a);
Node windNode = srcDoc.getElementsByTagName("wind").item(a);
Node bioNode = srcDoc.getElementsByTagName("bio").item(a);
RECORD = recordNode.getAttributes().getNamedItem("date").getNodeValue().toString();
TEMPERATURE = temperatureNode.getAttributes().getNamedItem("min").getNodeValue().toString();
WIND = windNode.getAttributes().getNamedItem("speed").getNodeValue().toString();
BIO = bioNode.getAttributes().getNamedItem("value").getNodeValue().toString();
Element element= (Element) srcDoc.getElementsByTagName("record").item(a);
NodeList status = element.getElementsByTagName("status");
NodeList text = element.getElementsByTagName("text");
Node statusNode = status.item(0);
Node textNode = text.item(0);
String statusText = statusNode.getFirstChild().getNodeValue();
String textText = textNode.getFirstChild().getNodeValue();
Log.e("Values", "STatus is " + statusText + " and text is " + textText);
}
}
这是xml文件
<root>
<forecast>
<record date="24-10-2012">
<temperature max="17" min="3" unit="°C" />
<wind speed="2 až 5" direction="SV" unit="m/s" />
<bio value="2" />
<status symbol="3">Fair</status>
<text>Very fair today.</text>
<warnings />
</record>
<record date="25-10-2012">
<temperature max="16" min="2" unit="°C" />
<wind speed="2 až 5" direction="Juhozápadný" unit="m/s" />
<bio value="3" />
<status symbol="3">Cloudy</status>
<text>Very cloudy tommorow. </text>
<warnings />
</record>
<record date="25-10-2012">
<temperature max="16" min="2" unit="°C" />
<wind speed="2 až 5" direction="Juhozápadný" unit="m/s" />
<bio value="3" />
<status symbol="3">Cloudy</status>
<text>Very cloudy tommorow. </text>
<warnings />
</record>
我得到了记录、温度、生物信息的输出,但没有状态和文本的输出。这个循环只会做一个。有谁能够帮我?