开发者

NSXML parsing loops 3 times over 1 node

开发者 https://www.devze.com 2023-03-12 03:34 出处:网络
I\'m using NSXMLParser to parse an XML file. I\'m doing this several times in my app for different purposes. I have one which parses annotations for a store locator, one that parses al the movies from

I'm using NSXMLParser to parse an XML file. I'm doing this several times in my app for different purposes. I have one which parses annotations for a store locator, one that parses al the movies from a certain YouTube channel and the last one parses a self made XML file.

All parsers work great except the last one. For some kind of reason it parses i.e. the first node good, but then overwrites it.

Here is my parser code:

    //Standard function parser: reading open tag
    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
      namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
        attributes:(NSDictionary *)attributeDict{ 
        currentElement = [elementName copy];
        if ([elementName isEqualToString:@"item"]) {
            xmlArray = [[NSMutableDictionary alloc] init];
        }

    }


    //Standard function parser: reading string
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
        if ([currentElement isEqualToString:@"created_time"]){
            [xmlArray setObject:string forKey:currentElement]; 
            NSLog(@"test 1 %@", string);}
    }    


    //Standard function parser: reading close tag
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
      namespaceURI:(NSString *)namespaceURI
     qualifiedName:(NSString *)qName{ 
        if ([elementName isEqualToString:@"item"]) {
            Post *newPost = [[Post alloc] init];
            NSLog(@"test %@", xmlArray );
            newPost.created_time = [xmlArray objectForKey:@"created_time"];
            [containerArray addObject:newPost];
            [xmlArray release];


 }
}  

The XML looks like this:

<?xml version="1.0" encoding="UTF-8" ?> 
<items>
<item> 
<created_time>test</created_time> 
<message>blabla</message> 
<picture>http://www.trentshow.com/images/tada.jpg</picture> 
</item>
</items>

And the log outputs the following:

2011-06-10 10:43:24.446 TabbedCalculation[1502:207] test 1 test
2011-06-10 10:43:24.448 TabbedCalculation[1502:207] test 1  
2011-06-10 10:43:24.449 TabbedCalculation[1502:207] test 1 
2011-06-10 10:43:24.450 TabbedCalculation[1502:207] test {
    "created_time" = "\n";
}

I'm not g开发者_JAVA百科etting why the parser loops 3 times over the created_time node. Any help would be great!


Try by following solution -- should work. foundCharacters can be called for multiple time for each key value --- basically you should append all found character list.

   - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
            if ([currentElement isEqualToString:@"created_time"]){
                NSString *valueKey = [xmlArray ValueForKey:forKey:currentElement];
                if(nil != valueKey) 
                {
                   valueKey = [valueKey stringByAppendingString:string];
                }else
                {
                   valueKey = string;
                }
                [xmlArray setObject:valueKey forKey:currentElement]; 
                NSLog(@"test 1 %@", string);}
        } 
0

精彩评论

暂无评论...
验证码 换一张
取 消