How do I use touchXML to parse this XML? I want to store all the attributes as key/value pairs in a dictionary.
For more information visit this post . I have given complete brief about it.
Yep ! solved your problem.
see, following code. Hope you understand. It's working for your requirement. I also have added - NSLog - Result - parsed.
-(void)methodForParsingPlayers{
NSMutableArray *ar=[[NSMutableArray alloc] init];
CXMLDocument *doc=[[[CXMLDocument alloc] initWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Players" ofType:@"xml"]] options:0 error:nil] autorelease];
NSArray *nodes=nil;
nodes=[doc nodesForXPath:@"//Player" error:nil];
NSString *strValue;
NSString *strName;
for (CXMLElement *node in nodes) {
NSMutableDictionary *object=[[NSMutableDictionary alloc] init];
// process to set attributes of object ----------------------------------------
NSMutableDictionary *objectAttributes=[[NSMutableDictionary alloc] init];
NSArray *arAttr=[node attributes];
NSUInteger i, countAttr = [arAttr count];
for (i = 0; i < countAttr; i++) {
strValue=[[arAttr objectAtIndex:i] stringValue];
strName=[[arAttr objectAtIndex:i] name];
if(strValue && strName){
[objectAttributes setValue:strValue forKey:strName];
}
}
[object setValue:objectAttributes forKey:[node name]];
[objectAttributes release]; objectAttributes=nil;
// --------------------------------------------------------------------------------
// process to read elements of object ----------------------------------------
NSUInteger j, countElements = [node childCount];
CXMLNode *element;
NSMutableDictionary *elementDictionary=nil;
for (j=0; j