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

发布于 2024-11-30 03:37:32 字数 5353 浏览 0 评论 0原文

<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 并获取节点上的数据? 请帮忙!

<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>

How can I parse above XML and get data on nodes?
Please Help !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挽袖吟 2024-12-07 03:37:32

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

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

There are a lot of tutorials on XML parsing, try this one its a good tutorial about xml parsing,

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

沉鱼一梦 2024-12-07 03:37:32

使用 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]
}

这样想

Use NSXMLParcer:

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

and than use 3 NSXMLParser delegate methods:

/**/
-(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]
}

somthink like that

归途 2024-12-07 03:37:32

有一个很好的 XML to NSDictionary 类,其中包含教程
http://troybrant.net/blog/2010/09/ simple-xml-to-nsdictionary-converter/

只需将其用于:

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

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

There is a nice little XML to NSDictionary class with tutorial on
http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/

just use it with:

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

And please be advised that WUnderground api is for personal non commercial usage only...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文