我无法使用来自SOAP
的Touch XML My响应来解析响应 <CXMLDocument 0x4b30110 [0x4b34c90]> <?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetImagesResponse xmlns="http://tempuri.org/">
<GetImagesResult>;http://www.google.com;http://www.hotmail.com;http://www.yahoo.com</GetImagesResult>
</GetImagesResponse>
</soap:Body>
</soap:Envelope>
///触摸XML功能
-(void) grabRSSFeed:(NSData *)responseData {
// Initialize the blogEntries MutableArray that we declared in the header
self.rssEntries = [[[NSMutableArray alloc] init] autorelease];
// Create a new rssParser object based on the TouchXML "CXMLDocument" class, this is the
// object that actually grabs and processes the RSS data
CXMLDocument *rssParser = [[CXMLDocument alloc] initWithData:responseData options:0 error:nil];
//CXMLDocument *rssParser = [[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil];
NSLog(@"rssParser %@",rssParser);
//NSLog(@"url %@",url);
// Create a new Array object to be used with the looping of the results from the rssParser
NSArray *resultNodes = NULL;
// Set the resultNodes Array to contain an object for every instance of an node in our RSS feed
resultNodes = [rssParser nodesForXPath:@"//GetImagesResult" error:nil];
// Loop through the resultNodes to access each items actual data
for (CXMLElement *resultElement in resultNodes) {
// Create a temporary MutableDictionary to store the items fields in, which will eventually end up in blogEntries
NSMutableDictionary *blogItem = [[[NSMutableDictionary alloc] init] autorelease];
// Create a counter variable as type "int"
int counter;
// Loop through the children of the current node
for(counter = 0; counter < [resultElement childCount]; counter++) {
//[resultElement initWithXMLString:@""];
NSString *strValue = [[resultElement childAtIndex:counter] stringValue];
//strValue = [strValue stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSString *strName = [[resultElement childAtIndex:counter] name];
if ([resultNodes containsObject:@""] || resultNodes == nil || resultElement == nil || [resultElement isEqual:@""] || [resultElement children] == nil) {
NSLog(@"Null Object");
}
else {
if (strValue && strName) {
// Add each field to the blogItem Dictionary with the node name as key and node value as the value
[blogItem setObject:[[resultElement childAtIndex:counter] stringValue] forKey:[[resultElement childAtIndex:counter] name]];
}
}
}
// Add the blogItem to the global blogEntries Array so that the view can access it.
[self.rssEntries addObject:[blogItem copy]];
NSLog(@"RSS Entries %@",self.rssEntries);
}
[[rssParser retain] release];
}
答案 0 :(得分:2)
“无法解析响应”是对您的问题的非常模糊的描述。但是,乍一看我发现这个XPath查询...
resultNodes = [rssParser nodesForXPath:@"//GetImagesResult" error:nil];
...很可能不匹配任何元素,因此返回一个空数组。
为什么呢? Your XML has a namespace。使用nodesForXPath:namespaceMappings:error:
方法代替nodesForXPath:error:
。前者有一个额外的参数,允许您提供命名空间映射字典。然后相应地编辑您的XPath查询。请参阅以下示例:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"http://tempuri.org/",
@"tempuri",
nil];
// Set the resultNodes Array to contain an object for every instance of an node in our RSS feed
resultNodes = [rssParser nodesForXPath:@"//tempuri:GetImagesResult" namespaceMappings:dict error:nil];
此外,该方法的最后一个陈述是无意义的:
[[rssParser retain] release];
要平衡alloc-init,它应该是:
[rssParser release];