繁体   English   中英

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

[英]How to get data from an URL xml-file?

我正在制作一个Android应用-需要天气信息的地方。 我是从Yahoo天气中找到的。 这是一种XML,我需要以下信息:“日”,“低”和“高”。

参考: 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. 拉出您想要的字段。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM