我正在尝试解析一些用于iphone应用程序的XML(IOS5 xcode4.2)我已经放弃了NSXMLParser,因为它无处可去。所以我使用touchXML,我能够从Web服务器中提取远程XML文件并查看节点名称,但这些节点的值是空白请参阅下面的代码和示例xml(我已经取出了大量数据)但是我是什么我想做的是获取一个NSArray或NSDirectory,其中包含所有统计数据的字符和子数组或NSDirectory的名称,它们键入的是stat的名称,值作为值
<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 = "";
}
)
答案 0 :(得分:0)
为了从节点中提取信息,您需要使用- (CXMLNode *)attributeForName:(NSString *)name
。我已修改您的代码以打印出所有所需的节点信息:
// Set the xml URL appropriately
NSURL *url = [NSURL URLWithString:@"file:///tmp/tmp.xml"];
// 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)
{
// Print out the initial character 'name' attribute
NSLog(@"Name: %@, Attribute: %@", [resultElement localName], [[resultElement attributeForName:@"name"] stringValue]);
// Loop through the children of the current node
for(int counter = 0; counter < [resultElement childCount]; counter++)
{
NSArray *children = [resultElement elementsForName:[[resultElement childAtIndex:counter] localName]];
for(int secondcounter = 0; secondcounter < [children count]; secondcounter++)
{
CXMLElement *child = [children objectAtIndex:secondcounter];
// Print out the subsequent layer's 'name' attributes
NSLog(@"Name: %@, Attribute: %@", [child localName], [[child attributeForName:@"name"] stringValue]);
// Special case for stats which have more children nodes
if([[child localName] isEqualToString:@"stats"])
{
NSArray *grandchildren = [child elementsForName:@"stat"];
for(int thirdcounter = 0; thirdcounter < [grandchildren count]; thirdcounter++)
{
CXMLElement *grandchild = [grandchildren objectAtIndex:thirdcounter];
// Get all of the stats name's
NSLog(@"Name: %@, Attribute: %@", [grandchild localName], [[grandchild attributeForName:@"name"] stringValue]);
}
}
}
}
}
来自NSLog:
2013-12-03 16:51:20.170 Validator[6520:707] Name: character, Attribute: testname
2013-12-03 16:51:20.171 Validator[6520:707] Name: vocation, Attribute: testvocation
2013-12-03 16:51:20.171 Validator[6520:707] Name: stats, Attribute: (null)
2013-12-03 16:51:20.171 Validator[6520:707] Name: stat, Attribute: health
2013-12-03 16:51:20.171 Validator[6520:707] Name: stat, Attribute: power
2013-12-03 16:51:20.172 Validator[6520:707] Name: equipment, Attribute: (null)