我无法使用GDataXML解析xml数据

起初我在.h文件中导入了#import "GDataXMLNode.h" 。 现在这是我的XML ,我必须使用GDataXML解析器进行解析。

  1 1 0 null 0 3 0  

现在,需要注意的一点是我的这个xml来自网络。 所以我使用NSUrlConnection委托来检索xml数据。

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding]; NSLog(@"Response: %@",responseString); // other stuff here... } 

在这里,我获得responseString ,然后使用以下代码解析它。 但是我无法解析它。

  xmlDocument = [[GDataXMLDocument alloc] initWithXMLString:responseString options:0 error:&errorOnStore]; if (nil == xmlDocument) { NSLog(@"could not load xml file"); } else { NSLog(@"Loading desire xml"); NSLog(@"%@", xmlDocument.rootElement); NSArray *getData = [[xmlDocument rootElement] elementsForName:@"SiteStats"]; NSLog(@"%@",getData); records = [[NSMutableArray alloc] init]; //[records retain]; if(getData.count !=0 ){ NSLog(@"data has"); } //storing the car model in the mutable array for(GDataXMLElement *e in getData){ NSLog(@"Enering in the xml file"); [records addObject:e]; NSString *Visitor = [[[e elementsForName:@"visitor"] objectAtIndex:0] stringValue]; NSLog(@"Visitor : %@",Visitor); NSString *UVisitor = [[[e elementsForName:@"uniqueVisitor"] objectAtIndex:0] stringValue]; NSLog(@"Unique Visitor : %@",UVisitor); } } 

我可以在NSLog(@"%@", xmlDocument.rootElement);时得到这个值NSLog(@"%@", xmlDocument.rootElement);

  GDataXMLElement 0x1a4290: {type:1 name:SiteStats xml:"4300030"} 

但我使用NSLog(@"%@",getData); 我在getData数组中没有得到任何数据。

谁能告诉我问题出在哪里? 先谢谢你。

 xmlDocument = [[GDataXMLDocument alloc] initWithXMLString:responseString options:0 error:&errorOnStore]; if (nil == xmlDocument) { NSLog(@"could not load xml file"); } else { NSLog(@"Loading desire xml"); NSLog(@"%@", xmlDocument.rootElement); // skip this bits, you're trying to retrieve wrong element //NSArray *getData = [[xmlDocument rootElement] elementsForName:@"SiteStats"]; //NSLog(@"%@",getData); records = [[NSMutableArray alloc] init]; //[records retain]; //if(getData.count !=0 ) //{ //NSLog(@"data has"); //} //storing the car model in the mutable array //for(GDataXMLElement *e in getData) //{ //NSLog(@"Enering in the xml file"); //[records addObject:e]; NSString *Visitor = [[[xmlDocument.rootElement elementsForName:@"visitor"] objectAtIndex:0] stringValue]; NSLog(@"Visitor : %@",Visitor); NSString *UVisitor = [[[xmlDocument.rootElement elementsForName:@"uniqueVisitor"] objectAtIndex:0] stringValue]; NSLog(@"Unique Visitor : %@",UVisitor); //} } 

编辑: NSArray *getData = [[xmlDocument rootElement] elementsForName:@"SiteStats"]; 在这一行中,你试图检索错误的元素,元素“SiteStats”已经是根元素,所以当你执行这些代码时,你要做的就是在“SideStats”中寻找“SiteStats”

   1 1 0 null 0 3 0   

如果您的XML看起来像上面那样,那么您的原始代码可能会起作用

要允许解析,您可能还需要更改标记中的属性“ xmlns

noNSxml 。 如果没有这样做,那么它将解析不正确。