如何使用目标c从xml获取数据?

问题描述:

我有XML结构如下如何使用目标c从xml获取数据?

<xml_api_reply version="1"> 
    <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0"> 
    <forecast_information> 
    <city data="Chennai, Tamil Nadu"/> 
    <postal_code data="chennai"/> 
    <forecast_date data="2011-12-13"/> 
    <current_date_time data="1970-01-01 00:00:00 +0000"/> 
    <unit_system data="US"/> 
    </forecast_information> 
    <current_conditions> 
    <condition data="Haze"/> 
    <temp_f data="84"/> 
    <temp_c data="29"/> 
    <humidity data="Humidity: 58%"/> 
    <icon data="/ig/images/weather/haze.gif"/> 
    <wind_condition data="Wind: NE at 13 mph"/> 
    </current_conditions> 
    </weather > 
</xml_api_reply> 

如何获取这些数据,并将其存储在一个数组?

+1

后你看一看的NSXMLParser模型文档? – V1ru8

+0

请提供有关该问题的足够信息。你想从服务器拉数据还是从本地文件读取数据? – Krishnan

+2

[如何使用目标c从xml中获取索引数据]的精确副本(http://*.com/questions/8486902/how-to-fetch-data-with-index-from-the-xml- using-objective-c) - 为什么再次发布相同的问题? – jrturton

其实你正在拥有属性的XML ......也很容易。您可以参考此Link: -

if([elementName isEqualToString:@"city"]) { 

    NSString *urlValue=[attributeDict valueForKey:@"data"]; 
} 

希望这可以帮助你...... :)) 如果没有的话告诉我,我会尝试更多的帮助你。

我会建议使用像KissXML(https://github.com/robbiehanson/KissXML)这样的框架来为你解析XML。

DDXMLDocument *myXmlDoc = [DDXMLDocument alloc] initWithXMLString:myXmlString options:options error:nil]; 

NSArray *elements = [[myXmlDoc rootElement] elementsWithName:@"forecast_information"]; 
... 
do stuff with the elements 

有关KissXML你可以阅读NSXML苹果的文档,如KissXML是这样