Search code examples
objective-cxmlsoapnsxmlparser

Parsing Group XML Node from SOAP Response - OBJECTIVE C


I have the following XML from SOAP Response how should I parse it ?

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
  <areaSearchResponse  soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
 <areaSearchReturn xsi:type="ns1:QueryBean" xmlns:ns1="http://rpc.xml.coldfusion">
 <details soapenc:arrayType="xsd:anyType[4]" xsi:type="soapenc:Array">
  <details xsi:type="soapenc:string">photo1.jpg</data>
  <details xsi:type="soapenc:int">1224</data>
  <details xsi:type="soapenc:double">552.0</data>
  <details xsi:type="soapenc:double">20.04</data>
 </details>
 <details soapenc:arrayType="xsd:anyType[4]" xsi:type="soapenc:Array">
  <details xsi:type="soapenc:string">photo2.jpg</data>
  <details xsi:type="soapenc:int">1234</data>
  <details xsi:type="soapenc:double">632.0</data>
  <details xsi:type="soapenc:double">34.05</data>
 </details>

I am using NSXMLParser as follows

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

if([[attributeDict valueForKey:@"xsi:type"] isEqualToString:@"soapenc:Array"]
   && [[attributeDict valueForKey:@"soapenc:arrayType"] isEqualToString:@"xsd:anyType[4]"]){

    _propertyInfoArray  =   [[NSMutableArray alloc] init];
    self.isNewRow   =   YES;
    NSLog(@" New Row Created ");
}else{
    self.isNewRow   =   NO;
    _rawData    =   [[NSMutableString alloc] init];
}

}

-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
[_rawData appendString:string];

}

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

    if(self.isNewRow){
        NSLog(@"Previous Row will be Added to NSSet");
    }else{
        [_propertyInfoArray addObject:_rawData];

        NSLog(@"Value %@ ",_rawData);
    }
}

}

In my above code in didEndElement: method I am always getting value of self.isNewRow as False (NO) Why is that ?

What other way I can parse the above XML ? Or What is the best way to parse the Group Node ?

Thanks in Advance !


Solution

  • I got it solved. I used KissXML and xPath to parse that XML. following is the code in case anybody is facing same issue to parse the xml

    DDXMLDocument *doc  =   [[DDXMLDocument alloc] initWithData:data options:0 error:&err];
    NSArray *areaSearchNode  =   [doc nodesForXPath:@"/soapenv:Envelope/soapenv:Body/areaSearchResponse/areaSearchReturn/details/" error:&err];
    
       for(DDXMLDocument *node in areaSearchNode){
        NSArray   *newRoot   =   node.children;
    
        DDXMLNode *photoURL =   (DDXMLNode*) newRoot[0];
    
        DDXMLNode *listingID =   (DDXMLNode*) newRoot[1];
        NSString *listingIDstr  =   [listingID stringValue];
    
        DDXMLNode *priceFrom =   (DDXMLNode*) newRoot[2];
        NSString *price =   [priceFrom stringValue];
    
        DDXMLNode *latNode =   (DDXMLNode*) newRoot[3];
        NSString *lat   =   [latNode stringValue];
    
        DDXMLNode *lonNode =   (DDXMLNode*) newRoot[4];
        NSString *lon   =   [lonNode stringValue];
    
        DDXMLNode *bedroomType =   (DDXMLNode*) newRoot[5];
        NSString *bedroomTypestr    =   [bedroomType stringValue];
    
        DDXMLNode *bathroomType =   (DDXMLNode*) newRoot[6];
        NSString *bathroomTypestr   =   [bathroomType stringValue];
    
       // Here adding parsed data into class
        IDSPropertyInfo *propertyInfo    =   [[IDSPropertyInfo alloc]
                                             initWithPhotoURL:[photoURL stringValue]
                                             listingID:[listingIDstr intValue]
                                             priceFrom:[price doubleValue]
                                             lat:[lat doubleValue]
                                             lon:[lon doubleValue]
                                             bedroomTypeID:[bedroomTypestr intValue]
                                             bathroomTypeID:[bathroomTypestr intValue]];
     }
    
      //Now Add IDSPropertyInfo Class into NSMutableArray.
     [self.propertyInfoArray addObject:propertyInfo];