one.html two.html Chapter" /> one.html two.html Chapter" /> one.html two.html Chapter"/>

NSXML Parser find parent's child nodes

281 Views Asked by At

I'm totally new to XML parse, have an XML structure like..

   <Chapter name = "chap1">
   <pages>one.html</pages>
   <pages>two.html</pages>
   </Chapter>
   Chapter name = "chap2">
   <pages>one.html</pages>
   <pages>two.html</pages>
   </Chapter>
   <Chapter name = "chap3">
   </Chapter>

I want to take each chapters pages separately... i.e if chapter has pages node i want to say YES else NO. How can i parse..I'm using NSXml Parser

2

There are 2 best solutions below

0
Burhanuddin Sunelwala On

You have to use NSXMLParser for that

look at the programming guide here : NSXMLParser Programming Guide

0
sathiamoorthy On

You have to try like this,

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
     if ([elementName isEqualToString:@"Chapter"]) 
     {
         ChapterOneFlag = YES;//use this as BOOL
     }
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSMutableString *)string
{
        buffContent=[buffContent stringByAppendingString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
      if (ChapterOneFlag == YES)
      {
        if ([elementName isEqualToString:@"pages"]) 
        { 
            [yourArr addObject:buffContent];
        }
     }
}

is this the code you want?. Here you can use youArr. It will come separately.