我有来自SOAP Response的以下XML如何解析它?
<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>
我正在使用NSXMLParser,如下所示
-(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);
}
}
}
在我上面的didEndElement:方法代码中,我总是将self.isNewRow的值变为False(NO) 那是为什么?
我可以用什么方法解析上面的XML? 要么 解析组节点的最佳方法是什么?
提前致谢!
答案 0 :(得分:1)
我解决了。我使用KissXML和xPath来解析XML。以下是代码,以防任何人面临同样的问题来解析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];