Search code examples
iphoneiosxmlparsingwsdl

WSDL/XML parsing iOS objective C


I would like some help to parse this Response. I think it is WSDL and would like to check "Successfully Entered" string. Below is the code:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://example.com/reg/abc">
   <soapenv:Header/>
   <soapenv:Body>
      <sch:LoginResponse>
         <sch:Status code=200>Successfully Entered</sch:Status>
         <sch:Message>[CDATA[Successfully Entered]]</sch:Message>
      </sch:LoginResponse>
   </soapenv:Body>
</soapenv:Envelope>

EDIT: Below is the code I have implemented

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
   attributes: (NSDictionary *)attributeDict
{
    NSLog(@"START ELEMENT NAME ==> %@", elementName);
    if( [elementName isEqualToString:@"sch:Status"])
    {
        if(!soapResults)
        {
            soapResults = [[NSMutableString alloc] init];
        }
        elementFound = YES;
    }
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if( elementFound )
    {
        [soapResults appendString:string];
    }
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    NSLog(@"END ELEMENT NAME ==> %@", elementName);
    if( [elementName isEqualToString:@"sch:Status"])
    {
        NSLog(@"Found ==> %@", soapResults);
        [soapResults setString:@""];
        elementFound = FALSE;
    }
}

Print log shows upto sch:LoginResponse and does not come to element name sch:Status


Solution

  • As per our chat, the problem is in the line:

    <sch:Status code=200>Successfully Entered</sch:Status>
    

    When code=200 is changed to code="200" it parses correctly.

    In other words, the XML is formatted incorrectly. Attributes are supposed to have either a single (') or double quotation (") around the data. (See W3C XML recommendaiton)

    Additionally, the delegate detects the error NSXMLParserAttributeNotStartedError in the XML when left unchanged:

    - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
        NSLog(@"Error: %@", parseError.localizedDescription);
    }
    

    If the XML must be retrieved this way, prior to parsing you can use this to surround it with quotation marks to make it valid, however you'd need to do it for each possible code=###:

    yourXMLString = [yourXMLString stringByReplacingOccurrencesOfString:@"code=200" withString:@"code=\"200\""];