0

一切都好。从互联网获取 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>

我得到了记录、温度、生物信息的输出,但没有状态和文本的输出。这个循环只会做一个。有谁能够帮我?

4

2 回答 2

0

status 和 text 没有任何子节点,因为它们不是节点列表,它们只是节点。尝试将它们作为节点并status.getNodeValue()改为使用。

于 2012-10-24T18:14:41.790 回答
0

I found the solution http://androidcodesnips.blogspot.sk/2011/05/dom-parsing-example.html

It doesnt look like so easy

于 2012-10-24T21:05:46.810 回答