如何解析android中的google weather xml数据

时间:2011-09-15 23:57:22

标签: android xml

我一直试图让它工作三个小时。这些是我尝试的两段代码和xml文件的链接。有人能告诉我我做错了什么或如何轻松做到这一点?我只想在“current_conditions”元素中将当前临时值存储为“temp_f”。如果有一个更好的方式来做这个比我想知道的任何尝试。非常感谢你!

这是我从网上下载的xml文件的链接: http://www.google.com/ig/api?weather=10598

这是我用来从网上获取xml数据的代码:

HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(url);
        // HttpGet get = new HttpGet();
        try {
            HttpResponse rsp = client.execute(get);
            String xmltemp = EntityUtils.toString(rsp.getEntity());
            System.out.println(xmltemp);

然后我尝试了这两个尝试解析xml但都没有工作。

        DocumentBuilderFactory factory1 = DocumentBuilderFactory
                .newInstance();
        factory1.setNamespaceAware(true); // never forget this!
        InputSource source = new InputSource(new StringReader(xmltemp));
        Document doc = factory1.newDocumentBuilder().parse(source);
        doc.getDocumentElement().normalize();
        System.out.println("Root element "
                + doc.getDocumentElement().getNodeName());
        Log.d("OUTPUT","Root element "
                + doc.getDocumentElement().getNodeName());
        NodeList nodeLst = doc.getElementsByTagName("weather");

        for (int s = 0; s < nodeLst.getLength(); s++) {

            Node fstNode = nodeLst.item(s);

            if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

                Element fstElmnt = (Element) fstNode;

                NodeList fstNmElmntLst = ((Document) fstElmnt)
                        .getElementsByTagName("module_id");
                Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
                NodeList fstNm = ((Node) fstNmElmnt).getChildNodes();
                System.out.println(((Node) fstNm.item(0)).getNodeValue());
                newal.add("" + ((Node) fstNm.item(0)).getNodeValue());
                Log.d("OUTPUT","" + ((Node) fstNm.item(0)).getNodeValue());
            }

        }

而且:

 DocumentBuilderFactory factory1 = DocumentBuilderFactory
             .newInstance(); factory1.setNamespaceAware(true); // never forget this!
             InputSource source = new InputSource(new
             StringReader(xmltemp)); Document doc =
             factory1.newDocumentBuilder().parse(source);

             XPathFactory factory11 = XPathFactory.newInstance(); XPath xpath1
             = factory11.newXPath(); XPathExpression expr1 = xpath1 .compile(
             "//forecast_information[postal_code='10598']/forecast_date/text()"
              ); Object result = expr1.evaluate(doc, XPathConstants.NODESET);

              NodeList nodes = (NodeList) result; for (int i = 0; i <
              nodes.getLength(); i++) {
              System.out.println(nodes.item(i).getNodeValue());
              Log.d("1010101010101",nodes.item(i).getNodeValue()); }

编辑: 所以这是我现在尝试使用的代码,但每次运行它我都会在logcat中收到此错误:

09-15 20:26:47.359: DEBUG/ErOr(17663): java.lang.ClassCastException: org.apache.harmony.xml.dom.ElementImpl

这是代码:

DocumentBuilderFactory factory1 = DocumentBuilderFactory.newInstance();
            factory1.setNamespaceAware(true); // never forget this!
            InputSource source = new InputSource(new StringReader(xmltemp));
            Document doc = factory1.newDocumentBuilder().parse(source);

            String newstring = "" + ((DocumentBuilderFactory) ((Document) doc.getDocumentElement().
               getElementsByTagName("current_conditions").item(0)).
               getElementsByTagName("temp_f").item(0)).
               getAttribute("data");

1 个答案:

答案 0 :(得分:0)

您需要了解的一件事是“module_id”不是一个元素,它是一个属性。因此,您无法使用getElementsByTagName(“module_id”)检索其值。此外,fstElmnt永远不会是一个文档 - 它是一个元素,因为你已经用getNodeType()确定了它。要获取module_id,您应该尝试:fstElement.getAttribute(“module_id”)。

但回到你的主要问题:要获得温度值,试试这个:

doc.getDocumentElement().
   getElementsByTagName("current_conditions").item(0).
   getElementsByTagName("temp_f").item(0).
   getAttribute("data");

我认为就是这样 - 未经测试!