Bharat
Bharat

Reputation: 3000

Parsing XML using NSXML for iPhone

I have an XML which has a structure like this:

<data>
   <books>
      <item>
          <title>book1</title>
          <author>author1</author>
      </item> 
      <item>
          <title>book2</title>
          <author>author2</author>
      </item>
   </books>
   <magazines>
      <item>
          <title>Mag1</title>
          <author>author1</author>
      </item>
   </magazines>
   <journals>
      <item>
          <title>Journal1</title>
          <author>author1</author>
      </item> 
      <item>
          <title>Journal2</title>
          <author>author2</author>
      </item>
   </journals>
</data>

I am using a table view to display the items of each category. I plan to have a table view with columns books, magazines & journals and clicking on them should display only the items under them. That is, if I click on books, it should only display the book entries.

I have this for xml parsing

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

    if ([elementName isEqual:@"books"]) {
        flag=1;
    }   

    if ([elementName isEqual:@"title"]) {   
        NSLog(@"found title!");
        parsedString = [[NSMutableString alloc] init];
    }
    if ([elementName isEqual:@"author"]) {  
        NSLog(@"description found");
        parsedString = [[NSMutableString alloc] init];
    }

}

and in

- (void)parser:(NSXMLParser *)parser
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
{
    if (flag==1){   
    if ([elementName isEqual:@"title"]) {
        [headlines addObject:parseString];

        [parseString release];
        parseString = nil;
    }

    if ([elementName isEqual:@"author"]) {
        [authors addObject:parsedString];
        [parseString release];
        parseString = nil;
        flag=2;
        }
    }
}

But this gets only the first entry of books and nothing else. Instead of setting flags is there a way to parse only between the <books> and </books> tags ?

Thank you.

Upvotes: 0

Views: 288

Answers (2)

Tim Dean
Tim Dean

Reputation: 8292

The reason it is only getting the first item is that you are setting flag to 2 when you see the end element for the author tag. I suspect you will get closer to what you want if you update this flag in the end element for the books tag, as follows:

- (void)parser:(NSXMLParser *)parser
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
{
  if (flag==1){   
    if ([elementName isEqual:@"title"]) {
        [headlines addObject:parseString];

        [parseString release];
        parseString = nil;
    }

    if ([elementName isEqual:@"author"]) {
        [authors addObject:parsedString];
        [parseString release];
        parseString = nil;
    }
    if ([elementName isEqual:@"books"]) {
        flag=2;
    }
  }
}

Upvotes: 1

Max
Max

Reputation: 16719

Just cancel parsing when the books tag ends:

- (void)parser:(NSXMLParser *)parser
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
{  
    if ([elementName isEqual:@"books"]) {
        [parser abortParsing];
    }

    ....
}

Upvotes: 0

Related Questions