2011-09-26 51 views
-1

我無法解析響應收到使用觸摸XML從SOAP我的回答無法使用TouchXML

<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]; 
} 
+5

究竟是什麼問題? – Vladimir

回答

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]; 
+0

感謝兄弟,它適用於我。 – user366584

+0

嗨@albertamg,現在我回答了一個新問題,之前在字符串中的響應,所以我成功地獲取它,現在響應是在xsd模式 – user366584

+0

@ user366584我建議你發佈一個新的問題與這個新的細節你面臨的問題。 – albertamg