我正在尝试为我放弃了 NSXMLParser 的 iphone 应用程序(IOS5 xcode4.2)解析一些 XML,因为去任何地方都很痛苦。所以我选择了 touchXML,我能够从 Web 服务器中提取我的远程 XML 文件并查看节点名称,但这些节点的值是空白的 请参阅下面的代码和示例 xml(我已经取出了很多数据)但是我我正在尝试做的是获取一个 NSArray 或 NSDirectory ,其中包含所有统计信息的字符名称和子数组或 NSDirectory ,它们的键是统计信息的名称,值是值
<apiresponse>
<character name="testname" ....>
<vocation name="testvocation" ....>...</vocation>
<stats>
<stat name="health" value="1234"/>
<stat name="power" value="4321"/>
</stats>
<equipment>
</equipment>
</character>
// Create a new xmlParser object based on the TouchXML "CXMLDocument" class, this is the
// object that actually grabs and processes the xml data
CXMLDocument *xmlParser = [[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil];
// Create a new Array object to be used with the looping of the results from the xmlParser
NSArray *resultNodes = NULL;
// Set the resultNodes Array to contain an object for every instance of an node in our xml
resultNodes = [xmlParser nodesForXPath:@"apiresponse/character" error:nil];
// Loop through the resultNodes to access each items actual data
for (CXMLElement *resultElement in resultNodes) {
// Create a temporary MutableDictionary to store the items fields in, which will eventually end up in stats
NSMutableDictionary *stat = [[NSMutableDictionary alloc] init];
// Create a counter variable as type "int"
int counter;
// Loop through the children of the current node
for(counter = 0; counter < [resultElement childCount]; counter++) {
// Add each field to the stat Dictionary with the node name as key and node value as the value
[stat setObject:[[resultElement childAtIndex:counter] stringValue] forKey: [[resultElement childAtIndex:counter] name]];
}
// Add the stat to the global stats Array so that the view can access it.
[self.stats addObject:[stat copy]];
}
}
NSLog
2012-01-25 08:27:08.890 test[12815:f803] (
{
equipment = "";
stats = "";
vocation = "";
}
)