raziiq
raziiq

Reputation: 549

NSXMLParser didEndElement and didStartElement been called multiple times

i am using NSXMLParser to parse xml data received from a web service in an app for iPad. My problem is didEndElement and didStartElement are been called multiple times (4 times to be precise).

Here are the methods

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

    NSLog(@"did start element");
    if( [elementName isEqualToString:@"WebServiceResult"])
    {

        if (!soapResults)
        {
             //NSLog(@"did start Element");
            soapResults = [[NSMutableString alloc] init];
        }
        elementFound = YES;
    }
}


-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    NSLog(@"did end element");

    if ([elementName isEqualToString:@"WebServiceResult"])
    {
        NSLog(@"Soap Results: %@", soapResults);
        [soapResults setString:@""];
        elementFound = FALSE; 
    }
}

Here is what i am parsing

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <authorizePassengerByEmailResponse xmlns="http://tempuri.org/">
      <authorizePassengerByEmailResult>string</authorizePassengerByEmailResult>
    </authorizePassengerByEmailResponse>
  </soap:Body>
</soap:Envelope>

Here string inside will either be a name like "FirstName LastName" or "Not Authorized".

Any suggestions on why this is happening?

Upvotes: 0

Views: 3081

Answers (2)

Nekto
Nekto

Reputation: 17877

It is correct behaviour. Look, you have 4 tags:

  1. <soap:Envelope...
  2. <soap:Body>
  3. <authorizePassengerByEmailResponse xmlns="http://tempuri.org/">
  4. <authorizePassengerByEmailResult>

So each time parser saw the open tag it calls didStartElement.

You will receive following stack of calls:

  • didStartElement : "soap"
  • didStartElement : "soap"
  • didStartElement : "authorizePassengerByEmailResponse"
  • didStartElement : "authorizePassengerByEmailResult"
  • didEndElement : "authorizePassengerByEmailResult"
  • didEndElement : "authorizePassengerByEmailResponse"
  • didEndElement : "soap"
  • didEndElement : "soap"

Upvotes: 2

Saranya
Saranya

Reputation: 1521

Check the following url for xml parsing:

http://gigaom.com/apple/tutorial-build-a-simple-rss-reader-for-iphone/

If you are parsing data from web.

Upvotes: -1

Related Questions