2011-12-13 58 views
-1

我有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://stackoverflow.com/questions/8486902/how-to-fetch-data-with-index-from-the-xml- using-objective-c) - 爲什麼再次發佈相同的問題? – jrturton

回答

0

其實你正在擁有屬性的XML ......也很容易。您可以參考此Link: -

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

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

希望這可以幫助你...... :)) 如果沒有的話告訴我,我會嘗試更多的幫助你。

0

我會建議使用像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是這樣