Android 台灣中文網

標題: 抓取XML來使用氣象APP [打印本頁]

作者: gn01871733    時間: 2012-9-12 14:44
標題: 抓取XML來使用氣象APP
不知道這個問題在這邊問對不對(汗

小弟最近使用了Yahoo Weather 提供的 XML 檔來讀取氣象資料

但不知道為甚麼

在抓取LocalName的時候只抓的到最後一個"guid"

XML在此 : http://weather.yahooapis.com/forecastrss?w=2306179

程式碼如下:
          public void startElement(String uri, String localName, String qName,
                        Attributes attributes) throws SAXException {
        // 地點
                if (localName.equals("yweather:location")) {
                        String value = attributes.getValue("city");
                        myWeatherInfo.Set_location(value);
                }
                // 圖片
                else if (localName.equals("yweather:condition")) {
                        String value = attributes.getValue("code");
                        myWeatherInfo.Set_weather_Icon(value);

                }
                // 溫度(C)
                else if (localName.equals("yweather:condition")) {
                        String value = attributes.getValue("temp");
                        myWeatherInfo.Set_temp(value);
                }
                // 濕度
                else if (localName.equals("yweather:atmosphere")) {
                        String value = attributes.getValue("humidity");
                        myWeatherInfo.Set_humpercent(value);
                }
                // 能見度
                else if (localName.equals("yweather:atmosphere")) {
                        String value = attributes.getValue("visibility");
                        myWeatherInfo.Set_vispercent(value);
                }
                // 日期
                else if (localName.equals("yweather:condition")) {
                                String value=attributes.getValue("date");
                                myWeatherInfo.Set_date(value);
        }

爬了不少文章,發現有些人也是差不多這樣的方式去寫
但不知道為甚麼整個XML只能抓到最後一個LocalName,也就是我沒有要使用的guid

先謝謝看文回文的各位了!!!








歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1