如何解析XML数据并存储在变量中?

时间:2011-08-17 08:34:55

标签: iphone xml nsxmlparser

<credit>Weather Underground NOAA Weather Station</credit>
    <credit_URL>http://wunderground.com/</credit_URL>
    <termsofservice link="http://www.wunderground.com/members/tos.asp#api" />
    <image>
    <url>http://icons-ecast.wxug.com/graphics/wu2/logo_130x80.png</url>
    <title>Weather Underground</title>
    <link>http://wunderground.com/</link>
    </image>
  <display_location>
  <full>کراÚÛ, Pakistan</full>
  <city>کراÚÛ</city>
  <state></state>
  <state_name>Pakistan</state_name>
  <country>PK</country>
  <country_iso3166>PK</country_iso3166>
  <zip>00000</zip>
  <latitude>24.89999962</latitude>
  <longitude>67.12999725</longitude>
  <elevation>22.00000000 ft</elevation>
  </display_location>
    <observation_location>
    <full>Karachi, </full>
    <city>Karachi</city>
    <state></state>
    <country>PK</country>
    <country_iso3166>PK</country_iso3166>
    <latitude>24.89999962</latitude>
    <longitude>67.12999725</longitude>
    <elevation>72 ft</elevation>
    </observation_location>
    <station_id>OPKC</station_id>
    <observation_time>Last Updated on August 17, 1:00 PM PKT</observation_time>
    <observation_time_rfc822>Wed, 17 Aug 2011 08:00:00 GMT</observation_time_rfc822>
    <observation_epoch>1313568000</observation_epoch>
    <local_time>August 17, 1:29 PM PKT</local_time>
    <local_time_rfc822>Wed, 17 Aug 2011 08:29:32 GMT</local_time_rfc822>
    <local_epoch>1313569772</local_epoch>
    <weather>Mostly Cloudy</weather>
    <temperature_string>90 F (32 C)</temperature_string>
    <temp_f>90</temp_f>
    <temp_c>32</temp_c>
    <relative_humidity>59%</relative_humidity>
    <wind_string>From the WSW at 18 MPH </wind_string>
    <wind_dir>WSW</wind_dir>
    <wind_degrees>250</wind_degrees>
    <wind_mph>18</wind_mph>
    <wind_gust_mph></wind_gust_mph>
    <pressure_string>29.56 in (1001 mb)</pressure_string>
    <pressure_mb>1001</pressure_mb>
    <pressure_in>29.56</pressure_in>
    <dewpoint_string>73 F (23 C)</dewpoint_string>
    <dewpoint_f>73</dewpoint_f>
    <dewpoint_c>23</dewpoint_c>

    <heat_index_string>98 F (37 C)</heat_index_string>
    <heat_index_f>98</heat_index_f>
    <heat_index_c>37</heat_index_c>


    <windchill_string>NA</windchill_string>
    <windchill_f>NA</windchill_f>
    <windchill_c>NA</windchill_c>

    <visibility_mi>4.3</visibility_mi>
    <visibility_km>7.0</visibility_km>
    <icons>
        <icon_set name="Default">
            <icon_url>http://icons-ecast.wxug.com/i/c/a/mostlycloudy.gif</icon_url>
        </icon_set>
        <icon_set name="Smiley">
            <icon_url>http://icons-ecast.wxug.com/i/c/b/mostlycloudy.gif</icon_url>
        </icon_set>
        <icon_set name="Generic">
            <icon_url>http://icons-ecast.wxug.com/i/c/c/mostlycloudy.gif</icon_url>
        </icon_set>
        <icon_set name="Old School">
            <icon_url>http://icons-ecast.wxug.com/i/c/d/mostlycloudy.gif</icon_url>
        </icon_set>
        <icon_set name="Cartoon">
            <icon_url>http://icons-ecast.wxug.com/i/c/e/mostlycloudy.gif</icon_url>
        </icon_set>
        <icon_set name="Mobile">
            <icon_url>http://icons-ecast.wxug.com/i/c/f/mostlycloudy.gif</icon_url>
        </icon_set>
        <icon_set name="Simple">
            <icon_url>http://icons-ecast.wxug.com/i/c/g/mostlycloudy.gif</icon_url>
        </icon_set>
        <icon_set name="Contemporary">
            <icon_url>http://icons-ecast.wxug.com/i/c/h/mostlycloudy.gif</icon_url>
        </icon_set>
        <icon_set name="Helen">
            <icon_url>http://icons-ecast.wxug.com/i/c/i/mostlycloudy.gif</icon_url>
        </icon_set>
        <icon_set name="Incredible">
            <icon_url>http://icons-ecast.wxug.com/i/c/k/mostlycloudy.gif</icon_url>
        </icon_set>
    </icons>
    <icon_url_base>http://icons-ecast.wxug.com/graphics/conds/</icon_url_base>
    <icon_url_name>.GIF</icon_url_name>
    <icon>mostlycloudy</icon>
    <forecast_url>http://www.wunderground.com/global/stations/41780.html</forecast_url>
    <history_url>http://www.wunderground.com/history/airport/OPKC/2011/8/17/DailyHistory.html</history_url>
    <ob_url>http://www.wunderground.com/cgi-bin/findweather/getForecast?query=24.89999962,67.12999725</ob_url>
</current_observation>

如何解析上面的XML并获取节点上的数据? 请帮助!

4 个答案:

答案 0 :(得分:2)

有很多关于XML解析的教程,试试这个关于xml解析的好教程,

http://www.edumobile.org/iphone/iphone-programming-tutorials/parsing-an-xml-file/

答案 1 :(得分:1)

使用NSXMLParcer:

NSXMLParser* parser = [[NSXMLParser alloc] initWithContentsOfURL:*someURL*]; // or you can load data directly
    [parser setDelegate:self];
    [parser parse];

并使用3个NSXMLParser委托方法:

/**/
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:*element header*]) {
        _stringData = [[NSMutableString alloc] init];
    }
}
/**/
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [_stringData appendFormat:@"%@",string];
}
/**/
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    *store info from String*
    [_stringData release]
}

喜欢那样的蠢事

答案 2 :(得分:1)

有一个很好的小XML到NSDictionary类和教程 http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/

只需使用它:

NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:XMLString error:&parseError];

请注意,WUnderground api仅用于个人非商业用途......

答案 3 :(得分:0)

使用TouchXML