Search code examples
objective-cnsxmlparser

Parsing XML elements for a TableView


At the moment I am designing a simple app that will read news feeds. The XML feed I am using is this... http://feeds.foxnews.com/foxnews/world?format=xml .

What I am looking to do is essentially take the 'title' element and the description' elements from this page, and use them in a TableView.

This is what I currently have:

-(void)parserDidStartDocument:(NSXMLParser *)parser{
    _elementsArray = [[NSMutableArray alloc]init];
}

-(void)parser:(NSXMLParser*)parser foundCharacters:(NSString *)string{
    if (!_currentString) {
        _currentString = [[NSMutableString alloc]init];
    }
    [_currentString appendString:string];
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString      *)namespaceURI qualifiedName:(NSString *)qName{

    if ([elementName isEqualToString:@"title"]) {
        [_elementsArray addObject:_currentString];
        _currentString=nil;
        return;

    }
    if ([elementName isEqualToString:@"description"]) {
        [_elementsArray addObject:_currentString];
        _currentString=nil;
        return;
    }
    _currentString = nil;
}

-(void)parserDidEndDocument:(NSXMLParser *)parser{
    for (NSString*string in _elementsArray) {
        NSLog(@"%@",string);
    }
    _elementsArray = nil;
}

I do get an output of this, that is not a problem.

My problem is that each element is added in a new NSArray entry. How would I add both elements to one entry? ( would it be possible to use keys ? )


Solution

  • You could use a temporary NSDictionary to store your title and description together for each item, and add the dictionary to the _elementsArray.

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
        if ([elementName isEqualToString:@"item"]) {
            _currentDict = [NSMutableDictionary dictionary];
            return;
        }
    }
    
    -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString      *)namespaceURI qualifiedName:(NSString *)qName{
        if ([elementName isEqualToString:@"item"]) {
            [_elementsArray addObject:_currentDict];
            _currentDict=nil;
            return;
        }
        if ([elementName isEqualToString:@"title"]) {
            _currentDict[@"title"] = _currentString;
            _currentString=nil;
            return;
    
        }
        if ([elementName isEqualToString:@"description"]) {
            _currentDict[@"description"] = _currentString;
            _currentString=nil;
            return;
        }
        _currentString = nil;
    }
    
    // Other methods left unchanged
    

    This should output something like:

    NSArray (
        NSDictionary {
            @"title": @"First title",
            @"description": @"A news article"
        },
        NSDictionary {
            @"title": @"Second title",
            @"description": @"Another interesting article"
        }
    )
    

    Untested, but I hope you get the idea.

    Note also that parsing an XML (or RSS) file into native ObjC objects is a very recurring problem, and lots of open source libs out there already do most of the heavy lifting for you.