[英]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。 所以,你去
我想這已經完成了。 (在Google上很容易找到) http://www.javahouse.altervista.org/gambino/Articolo_lettura_feed_da_java_en.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.