- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0">
- <channel>
- <title>Yahoo! Weather - Chengdu, CH</title>
- <link>http://us.rd.yahoo.com/dailynews/rss/weather/Chengdu__CH/*http://weather.yahoo.com/forecast/CHXX0016_c.html</link>
- <description>Yahoo! Weather for Chengdu, CH</description>
- <language>en-us</language>
- <lastBuildDate>Wed, 03 Aug 2011 10:00 am CST</lastBuildDate>
- <ttl>60</ttl>
- <yweather:location city="Chengdu" region="" country="CH"/>
- <yweather:units temperature="C" distance="km" pressure="mb" speed="km/h"/>
- <yweather:wind chill="23" direction="0" speed="0"/>
- <yweather:atmosphere humidity="78" visibility="9.99" pressure="982.05" rising="0"/>
- <yweather:astronomy sunrise="6:22 am" sunset="7:57 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 Chengdu, CH at 10:00 am CST</title>
- <geo:lat>30.67</geo:lat>
- <geo:long>104.02</geo:long>
- <link>http://us.rd.yahoo.com/dailynews/rss/weather/Chengdu__CH/*http://weather.yahoo.com/forecast/CHXX0016_c.html</link>
- <pubDate>Wed, 03 Aug 2011 10:00 am CST</pubDate>
- <yweather:condition text="Partly Cloudy" code="30" temp="23" date="Wed, 03 Aug 2011 10:00 am CST"/>
- <description><![CDATA[
- <img src="http://l.yimg.com/a/i/us/we/52/30.gif"/><br />
- <b>Current Conditions:</b><br />
- Partly Cloudy, 23 C<BR />
- <BR /><b>Forecast:</b><BR />
- Wed - Showers. High: 23 Low: 20<br />
- Thu - Thunderstorms. High: 26 Low: 21<br />
- <br />
- <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Chengdu__CH/*http://weather.yahoo.com/forecast/CHXX0016_c.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="Wed" date="3 Aug 2011" low="20" high="23" text="Showers" code="11"/>
- <yweather:forecast day="Thu" date="4 Aug 2011" low="21" high="26" text="Thunderstorms" code="4"/>
- <guid isPermaLink="false">CHXX0016_2011_08_03_10_00_CST</guid>
- </item>
- </channel>
- </rss>
- <!-- api6.weather.sp2.yahoo.com uncompressed/chunked Tue Aug 2 19:19:00 PDT 2011 -->
解析XML
//解析天气
- (void)XMLPaser:(NSString *)XMLSourceStr
{
weatherItemsDic_ = [[NSMutableDictionaryalloc] init];
//替换字段“yweather:”为空字符
NSString *tempXMLSourceStr = [XMLSourceStrstringByReplacingOccurrencesOfString:@"yweather:"withString:@""];
CXMLDocument *doc = [[[CXMLDocumentalloc] initWithXMLString:tempXMLSourceStroptions:0 error:nil] autorelease];
NSArray *nodes = [NSArrayarray];
nodes = [docnodesForXPath:@"//item"error:nil];
for (CXMLElement *nodein nodes) {
[weatherItemsDic_setObject:[selfpaserForCity:[[[nodeelementsForName:@"title"]objectAtIndex:0]stringValue]] forKey:@"city_key"];
[weatherItemsDic_setObject:[[[[nodeelementsForName:@"condition"]objectAtIndex:0]attributeForName:@"temp"]stringValue] forKey:@"current_temp_key"];
[weatherItemsDic_setObject:[[[[nodeelementsForName:@"forecast"]objectAtIndex:0]attributeForName:@"low"]stringValue] forKey:@"today_low_key"];
[weatherItemsDic_setObject:[[[[nodeelementsForName:@"forecast"]objectAtIndex:0]attributeForName:@"high"]stringValue] forKey:@"today_high_key"];
[weatherItemsDic_setObject:[[[[nodeelementsForName:@"condition"]objectAtIndex:0]attributeForName:@"code"]stringValue] forKey:@"today_image_code"];
//NSLog(@"********* Condition: %@ *******",[[[[node elementsForName:@"condition"] objectAtIndex:0]attributeForName:@"text"] stringValue]);
//NSLog(@"*********YYY %@ *******",[[[[node elementsForName:@"condition"] objectAtIndex:0]attributeForName:@"code"] stringValue]);
//NSLog(@"********* current temperature: %@ *******",[[[[node elementsForName:@"condition"] objectAtIndex:0]attributeForName:@"temp"] stringValue]);
//NSLog(@"********* date: %@ *******",[[[[node elementsForName:@"condition"] objectAtIndex:0]attributeForName:@"date"] stringValue]);
//
////第一天的预报
//NSLog(@"********* 1date: %@ *******",[[[[node elementsForName:@"forecast"] objectAtIndex:0]attributeForName:@"day"] stringValue]);
//NSLog(@"********* date: %@ *******",[[[[node elementsForName:@"forecast"] objectAtIndex:0]attributeForName:@"date"] stringValue]);
//NSLog(@"********* date: %@ *******",[[[[node elementsForName:@"forecast"] objectAtIndex:0]attributeForName:@"low"] stringValue]);
//NSLog(@"********* date: %@ *******",[[[[node elementsForName:@"forecast"] objectAtIndex:0]attributeForName:@"high"] stringValue]);
//NSLog(@"********* date: %@ *******",[[[[node elementsForName:@"forecast"] objectAtIndex:0]attributeForName:@"text"] stringValue]);
//NSLog(@"********* date: %@ *******",[[[[node elementsForName:@"forecast"] objectAtIndex:0]attributeForName:@"code"] stringValue]);
////第二天的预报
//NSLog(@"********* 1date: %@ *******",[[[[node elementsForName:@"forecast"] objectAtIndex:1]attributeForName:@"day"] stringValue]);
//NSLog(@"********* date: %@ *******",[[[[node elementsForName:@"forecast"] objectAtIndex:1]attributeForName:@"date"] stringValue]);
//NSLog(@"********* date: %@ *******",[[[[node elementsForName:@"forecast"] objectAtIndex:1]attributeForName:@"low"] stringValue]);
//NSLog(@"********* date: %@ *******",[[[[node elementsForName:@"forecast"] objectAtIndex:1]attributeForName:@"high"] stringValue]);
//NSLog(@"********* date: %@ *******",[[[[node elementsForName:@"forecast"] objectAtIndex:1]attributeForName:@"text"] stringValue]);
//NSLog(@"********* date: %@ *******",[[[[node elementsForName:@"forecast"] objectAtIndex:1]attributeForName:@"code"] stringValue]);
}
}
TOUCHXML解析xml