1
我試圖解析一些XML爲我放棄了用的NSXMLParser iPhone應用程序(IOS5 xcode4.2),因爲它是一個痛苦取得任何進展。所以我touchXML去了,我可以從Web服務器拉我的遠程XML文件,並看到節點的名稱,但這些節點的值低於空白見代碼示例XML(我已經採取了大量的數據),但我我試圖做的就是一個的NSArray或NSDirectory與人物和他們鍵爲統計的名稱和值作爲值TouchXML空節點值
<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]];
}
}
的所有統計數據的子陣列或NSDirectory的名字
的NSLog
2012-01-25 08:27:08.890 test[12815:f803] (
{
equipment = "";
stats = "";
vocation = "";
}
)
你是說,你只關心'stats'部分?你不關心設備或職業?同樣在例子中,'是''testname「'字符的名字? –
NJones
是的,我關心的是,裝備和職業人,是的測試名稱是字符的名稱 – GerritVK