Hashmat Khalil
Hashmat Khalil

Reputation: 1816

nsxmlparser read the root element

I'm trying to get the root element. based on the root element I want to trigger a function.

For exmaple the xml looks like this:

  <State>
     <Name>California</Name>
     <Time>CA Time.</Time>
     <Time>CA Time2.</Time>
     <Notes>This is a note for California</Notes>
  </State>

and the next incoming xml looks like this:

  <country>
     <Name>USA</Name>
     <Time>west coast Time.</Time>
  </country>

so based on the the root element i want to trigger the right function. I'm using the current NSXmlparser delegate methods.

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

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

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;

but it seems its skipping the the root element. od did i miss any method which can get the the root element first?

Upvotes: 0

Views: 506

Answers (1)

ipinak
ipinak

Reputation: 6039

Check this out:

- (void)parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName 
attributes:(NSDictionary *)attributeDict
{    
    currentKey = nil;
    [currentStringValue release];
    currentStringValue = nil;
    if([elementName isEqualToString:@"State"]){
       // Here you got the your root...
    }
}

- (void)parser:(NSXMLParser *)parser 
foundCharacters:(NSString *)string
{
    if(currentKey){
        if(!currentStringValue){ // Here you got the contents...
            // Store them somewhere in case you need them...
            currentStringValue = [[NSMutableString alloc] initWithCapacity:200];
        }
        [currentStringValue appendString:string];
    }
}

-(void)parser:(NSXMLParser *)parser 
didEndElement:(NSString *)elementName 
namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"State"]){
        // Do what you want here...
        return;
    }
}

And here is a link from apple, always read the documentation and their code examples... http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/NSXML_Concepts/NSXML.html#//apple_ref/doc/uid/TP40001263-SW1

I'm not 100% sure, but I think it works...

Accordingly you can make the change for the county.

Upvotes: 4

Related Questions