Чтобы вытащить информацию из узла, вам нужно будет использовать - (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)