TouchXML parsing XML attributes

后端 未结 3 1340
我在风中等你
我在风中等你 2020-12-29 14:16

How do I use touchXML to parse this XML? I want to store all the attributes as key/value pairs in a dictionary.



        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-29 15:05

    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

提交回复
热议问题