0

出于学习目的,我在 android 中制作了一个简单的演示天气应用程序,我有 1 个 api(yahoo 天气 api)用于从中解析数据,任何朋友都可以告诉我如何获取“yweather”的“代码”值;预测” xml 标签的每一天?我的 api 喜欢是yahoo 天气 api 我的 xml 如下: 天气:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
        <rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
            <channel>

<title>Yahoo! Weather - Dubai, AE</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Dubai__AE/*http://weather.yahoo.com/forecast/AEXX0004_f.html</link>
<description>Yahoo! Weather for Dubai, AE</description>
<language>en-us</language>
<lastBuildDate>Fri, 03 Jan 2014 7:58 am GST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Dubai" region=""   country="United Arab Emirates"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
<yweather:wind chill="63"   direction="180"   speed="3" />
<yweather:atmosphere humidity="77"  visibility="6.21"  pressure="30.18"  rising="1" />
<yweather:astronomy sunrise="7:03 am"   sunset="5:38 pm"/>
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif</url>
</image>
<item>
<title>Conditions for Dubai, AE at 7:58 am GST</title>
<geo:lat>25.27</geo:lat>
<geo:long>55.31</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Dubai__AE/*http://weather.yahoo.com/forecast/AEXX0004_f.html</link>
<pubDate>Fri, 03 Jan 2014 7:58 am GST</pubDate>
<yweather:condition  text="Fair"  code="34"  temp="63"  date="Fri, 03 Jan 2014 7:58 am GST" />
<description><![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/34.gif"/><br />
<b>Current Conditions:</b><br />
Fair, 63 F<BR />
<BR /><b>Forecast:</b><BR />
Fri - Sunny. High: 73 Low: 61<br />
Sat - Sunny. High: 73 Low: 58<br />
Sun - Sunny. High: 75 Low: 62<br />
Mon - AM Clouds/PM Sun. High: 76 Low: 62<br />
Tue - Scattered Showers. High: 69 Low: 59<br />
Wed - Partly Cloudy. High: 73 Low: 58<br />
Thu - Partly Cloudy. High: 72 Low: 58<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Dubai__AE/*http://weather.yahoo.com/forecast/AEXX0004_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]></description>
<yweather:forecast day="Fri" date="3 Jan 2014" low="61" high="73" text="Sunny" code="32" />
<yweather:forecast day="Sat" date="4 Jan 2014" low="58" high="73" text="Sunny" code="32" />
<yweather:forecast day="Sun" date="5 Jan 2014" low="62" high="75" text="Sunny" code="32" />
<yweather:forecast day="Mon" date="6 Jan 2014" low="62" high="76" text="AM Clouds/PM Sun" code="30" />
<yweather:forecast day="Tue" date="7 Jan 2014" low="59" high="69" text="Scattered Showers" code="39" />
<yweather:forecast day="Wed" date="8 Jan 2014" low="58" high="73" text="Partly Cloudy" code="30" />
<yweather:forecast day="Thu" date="9 Jan 2014" low="58" high="72" text="Partly Cloudy" code="30" />
<guid isPermaLink="false">AEXX0004_2014_01_09_7_00_GST</guid>
</item>
</channel>
</rss>

<!-- fan474.sports.gq1.yahoo.com Fri Jan  3 04:36:49 PST 2014 -->

请帮帮我

4

4 回答 4

0

您可以立即复制此代码。

    private class Weather{
    public String day;
    public String date;
    public String low;
    public String high;
    public String text;
    public String code;
}
private void xmlParsingDemo() throws Exception{
    // init
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser parser = factory.newPullParser();
    String path = getFilesDir() + File.separator + "weather.xml";
    parser.setInput(new FileInputStream(path), "utf-8");
    // start to parse
    List<Weather> weathers = new ArrayList<Weather>();
    Weather weather = null;
    int eventType = parser.getEventType();
    while(eventType != XmlPullParser.END_DOCUMENT){
        if(eventType == XmlPullParser.START_TAG && "forecast".equals(parser.getName())){
            weather = new Weather();
            weather.day = parser.getAttributeValue(null, "day");
            weather.date = parser.getAttributeValue(null, "date");
            weather.low = parser.getAttributeValue(null, "low");
            weather.high = parser.getAttributeValue(null, "high");
            weather.text = parser.getAttributeValue(null, "text");
            weather.code = parser.getAttributeValue(null, "code");
            weathers.add(weather);
        }
        eventType = parser.next();
    }

    //by now
    //the list weathers is all of the weather you want
}
于 2014-01-03T08:54:30.723 回答
0

xml解析可以参考以下链接(Android推荐XmlPullParser):http: //developer.android.com/training/basics/network-ops/xml.html

于 2014-01-03T08:10:11.953 回答
0
XmlPullParser xpp = getResources().getXml(R.xml.answer);
    try {
        while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
            switch (xpp.getEventType()) {
            case XmlPullParser.START_TAG:
                if (xpp.getName().equals("forecast")) {
                    for (int i = 0; i < xpp.getAttributeCount(); i++) {
                        if (xpp.getAttributeName(i).equals("code"))
                            Log.d("myLogs", "code=" + xpp.getAttributeValue(i));
                    }
                    break;
                }
            default:
                break;
            }
            xpp.next();
        }
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

XmlPullParser 方法getName()忽略 xml 命名空间,所以如果你尝试xpp.getName()在 xml 上执行,比如

<yweather:forecast day="Fri" date="3 Jan 2014" low="61" high="73" text="Sunny" code="32" />

它只会返回“预测”。

于 2014-01-03T08:14:22.920 回答