如何解析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>
如何解析上面的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
关于 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/
使用 NSXMLParcer:
然后使用 3 个 NSXMLParser 委托方法:
这样想
Use NSXMLParcer:
and than use 3 NSXMLParser delegate methods:
somthink like that
有一个很好的 XML to NSDictionary 类,其中包含教程
http://troybrant.net/blog/2010/09/ simple-xml-to-nsdictionary-converter/
只需将其用于:
请注意,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:
And please be advised that WUnderground api is for personal non commercial usage only...
使用 TouchXML。
Use TouchXML.