Unable to parser using TouchXML
I am unable to parse response recieve using Touch XML My response from SOAP
<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>;h开发者_JAVA百科ttp://www.google.com;http://www.hotmail.com;http://www.yahoo.com</GetImagesResult>
</GetImagesResponse>
</soap:Body>
</soap:Envelope>
/// Touch XML function
-(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];
}
Being "unable to parse response" is a very vague description of your problem. However, at first glance I noticed that this XPath query...
resultNodes = [rssParser nodesForXPath:@"//GetImagesResult" error:nil];
...is most likely not matching any elements and thus returning an empty array.
Why? Your XML has a namespace. Use nodesForXPath:namespaceMappings:error:
method instead of nodesForXPath:error:
. The former has an extra parameter that lets you provide a namespace mapping dictionary. Then edit your XPath query accordingly. See the example below:
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];
Also, the last statement of the method is nonsense:
[[rssParser retain] release];
To balance the alloc-init, it should be:
[rssParser release];
精彩评论