如何从URL xml文件中获取数据



我正在制作一个android应用程序,在那里我需要天气信息。我从雅虎的天气里找到了这个。它是一个XML,我想要诸如"day"、"low"one_answers"high"之类的信息。

参考:http://weather.yahooapis.com/forecastrss?w=12718298&u=c

<yweather:forecast day="Sun" date="19 Feb 2012" low="-2" high="3" text="Clear" code="31"/>

(可在链接底部找到行)

我不知道该怎么做——请帮忙。源代码、示例和线索将不胜感激。

以下是面向未来用户的解决方案:

 InputStream inputXml = null;
    try
    {

             inputXml  = new URL("http://weather.yahooapis.com/forecastrss?w=12718298&u=c").openConnection().getInputStream();

       DocumentBuilderFactory factory = DocumentBuilderFactory.
                                        newInstance();
       DocumentBuilder builder = factory.newDocumentBuilder();
       Document doc = builder.parse(inputXml);
       NodeList nodi = doc.getElementsByTagName("yweather:forecast");
       if (nodi.getLength() > 0)
       {

          Element nodo = (Element)nodi.item(0);
          String strLow = nodo.getAttribute("low");
          Element nodo1 = (Element)nodi.item(0);
          String strHigh = nodo1.getAttribute("high");
          System.out.println("Temperature low: " + strLow);
          System.out.println("Temperature high: " + strHigh);
        }
    }
    catch (Exception ex)
    {
       System.out.println(ex.getMessage());
    }
    finally
    {
       try
       {
          if (inputXml != null)
          inputXml.close();
       }
       catch (IOException ex)
       {
          System.out.println(ex.getMessage());
       }
    }
 }

我在Android中使用XML已经有几年了,但当我刚开始使用时,这对我很有帮助:anddev.org

链接似乎是一个提要。(显然是XML)。Java中有许多提要阅读器API。所以,给你

  1. 阅读提要文档,http://developer.yahoo.com/weather/
  2. 阅读如何解析/阅读提要,罗马图书馆阅读提要Java
  3. 拉出您想要的字段

我想这已经完成了。(在谷歌上很容易找到)http://www.javahouse.altervista.org/gambino/Articolo_lettura_feed_da_java_en.html

最新更新