0

Here is the case, title exist in two occurrences, before and after entry:

<feed xmlns:im="http://xxx.com/rss" xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <id>http://xxxxx/xml</id><title>xxxxx: Top articles</title><updated>2013-07-20T04:30:05-07:00</updated><link rel="alternate" type="text/html" href="https://xxxx;popId=1"/><link rel="self" href="http://xxxxxxx/xml"/><icon>http://xxxxxxxxxxx</icon><author><name>xxxxxxx</name><uri>http://www.xxxxx.com/xxxxx/</uri></author><rights>Copyright 2001</rights>

        <entry>
            <updated>2013-07-20T04:30:05-07:00</updated>

                <id im:id="621507457">https://xxxxx.xxxx.com/us/album/blurred-lines-feat.-t.i.-pharrell/id621507456?i=621507457&amp;uo=2</id>
                 //I want to get only the title nested in entry tree
                <title>Robin Thicke</title>

What test should be made in didStartElement: protocol method in order to escape any outside the <entry></entry>?

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{

    if ([elementName isEqualToString:@"title"]) {
      //This is not enough, since it bring also the title value which is outside the entry tree
    }
}

I don't want to rely on an external library like TBXML, I want to know wether this is doable in pure NSXMLParserDelegate?

Malloc
  • 15,434
  • 34
  • 105
  • 192
  • I am not going to answer your question, but give you a list of "better" xml parsers: http://www.raywenderlich.com/553/xml-tutorial-for-ios-how-to-choose-the-best-xml-parser-for-your-iphone-project – Rui Peres Jul 20 '13 at 15:20
  • I already read that article, I need a way to do so with pure NSXMLParserDelegate without relying to another library. Thanx. – Malloc Jul 20 '13 at 15:22

1 Answers1

0

You have to keep track of whether the parser is currently "inside" an entry element or not. For example, you can add an integer property entryLevel to the class, set it initially to zero and update it in didStartElement and didEndElement:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"entry"]) {
        self.entryLevel++;
    } else if ([elementName isEqualToString:@"title"] && self.entryLevel > 0) {
        // title inside entry
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"entry"]) {
        self.entryLevel--;
    }
}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Hi, I will accept this solution as long a it appears to me there is no other way to test nested levels. – Malloc Jul 20 '13 at 15:36