2010-05-06 50 views

回答

2

退房TouchXML。這是迄今爲止我發現的最簡單的圖書館。支持基本級別的xpath。 http://code.google.com/p/touchcode/wiki/TouchXML。以下是一個最近項目的示例(代碼已刪除):

CXMLDocument *parser = [[CXMLDocument alloc] initWithData:theData encoding:NSUTF8StringEncoding options:0 error:nil]; 
NSArray *nodes = [parser nodesForXPath:@"//item" error:nil]; 

for (CXMLElement *resultElement in nodes) 
{ 
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithCapacity:0]; 

    // Create a counter variable as type "int" 
    int counter; 

    // Loop through the children of the current node and add to dictionary 
    for (counter = 0; counter < [resultElement childCount]; counter++) 
    { 
     // Add each field to the dictionary with the node name as 
     // key and node value as the value 
     [data setObject:[[resultElement childAtIndex:counter] stringValue] 
       forKey:[[resultElement childAtIndex:counter] name]]; 
    } 

    // do stuff with the dictionary by named keys 
    // ... 

    // release dict 
    [data release]; 
} 

[parser release]; 
0

查看NSXmlParser。它可以讀取xml併爲您提供可覆蓋的方法來處理內容。

1

如果您知道一些C,解析XML到Objective-C對象中的最佳(即最快分析,最小內存佔用)方式可能是通過libxml2中的SAX事件解析器。 Apple有一個名爲XMLPerformance的示例項目,演示了這是如何完成的。

1

我推薦使用TouchXML。與NSXMLParser相比,優秀的文檔並且相對易於使用。