【问题标题】:How to parse google weather xml data in android如何在android中解析谷歌天气xml数据
【发布时间】:2011-09-15 23:57:22
【问题描述】:

我已经尝试让它工作了大约三个小时。这是我尝试的两段代码和 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");

【问题讨论】:

    标签: android xml


    【解决方案1】:

    您需要了解的一点是“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");
    

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

    【讨论】:

    • 我试过了,但 getChildNodes 说“节点类型中的方法 getChildNodes() 不适用于参数(字符串)”
    • 你说得对 - 面对手掌 - DOM API 真的很糟糕 - 我已经习惯使用 JDOM,这更明智。我会编辑答案...
    • 谢谢...也许这是一个愚蠢的问题,但我如何将这些数据转换成一个字符串,以便我可以测试它是否真的有效?
    • 我在答案中输入的表达式返回一个字符串。我想这就是你需要的,对吧?
    • 是的,对不起...我在 logcat "09-15 20:26:47.359: DEBUG/ErOr(17663): java.lang.ClassCastException: org.apache.harmony. xml.dom.ElementImpl "
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    相关资源
    最近更新 更多