0

I have a xml file, i can do the parsing using the NSXML parser. Now i want to start parsing from specific tag , how this can be done ?

<RootElement>
<City>
<Name>WC</Name>
<Time>6 am</Time>
<Notes>Hi</Notes>
</City>
  <State>
    <Name>ABC</Name>
    <Time>6 a.m.</Time>
    <Time>2 a.m.</Time>
    <Notes>This is a note for ABC</Notes>
  </State>
  <State>
    <Name>DEF</Name>
    <Time>8 am</Time>
    <Time>10 am</Time>
    <Notes>This is a note for DEF</Notes>
  </State>
</RootElement>

Now the parsing should start from State tag , but it parses entire document and displays the name,time,notes from the City Tag also. Parsing from specified tag , how it can be done ?

Cyril
  • 1,216
  • 3
  • 19
  • 40

1 Answers1

1

Take a look at:

- (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error

EDIT: Code example

NSError* error;

CXMLDocument *doc = [[CXMLDocument alloc] initWithData:Data options:0 error:&error];

// figure out what nodes are in the doc, and pick it apart with the nodesForXPath call
NSArray * results =  [doc nodesForXPath:@"/RootElement" error:&error];
NSString * finalUrl = self.defaultURL;

if ( [results count] > 0 ) {
    NSString  * element = [(CXMLElement *)[results objectAtIndex:0] stringValue];
    if ( [element isEqualToString:@"State") {
        // Do state processing
    }
}
[doc release];
Rayfleck
  • 12,116
  • 8
  • 48
  • 74
  • little Confused, I am a Novice, please tell me how can i use that in didStartElement ? – Cyril Feb 03 '12 at 04:47
  • I added some sample code. Also take a look at this post: http://stackoverflow.com/questions/8055817/nsxmldocument-search-with-nodesforxpath – Rayfleck Feb 03 '12 at 23:09