2012-01-24 26 views
0

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

<SiteStats> 
    <visitor>1</visitor> 
    <uniqueVisitor>1</uniqueVisitor> 
    <orderCount>0</orderCount> 
    <revenue>null</revenue> 
    <conversionRate>0</conversionRate> 
    <newProduct>3</newProduct> 
    <outOfStockProduct>0</outOfStockProduct> 
</SiteStats> 

现在,有一点要注意的是,我的这个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);

GDataXMLElement 0x1a4290: {type:1 name:SiteStats xml:"<SiteStats><visitor>4</visitor><uniqueVisitor>3</uniqueVisitor><orderCount>0</orderCount><revenue>0</revenue><conversionRate>0</conversionRate><newProduct>3</newProduct><outOfStockProduct>0</outOfStockProduct></SiteStats>"} 

但我用NSLog(@"%@",getData);我不getData阵列得到任何数据,我可以得到这个值。

有人可以告诉我问题在哪里吗?先谢谢你。

+0

错误在哪里?也许张贴您的NSLog的输出以及 –

+0

'NSLog(@“%@”,getData);'print'(null)'。请问一下,有人如何帮助我。 –

+0

@XSlash我编辑了我的代码。 –

回答

1
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"];在这一行,你要检索错误的元素,元素“网站统计”已经是根元素,所以当你的代码位,你正在试图做的正在寻找“网站统计”,“SideStats”内部

<SiteStats> 
    <SiteStats> 
     <visitor>1</visitor> 
     <uniqueVisitor>1</uniqueVisitor> 
     <orderCount>0</orderCount> 
     <revenue>null</revenue> 
     <conversionRate>0</conversionRate> 
     <newProduct>3</newProduct> 
     <outOfStockProduct>0</outOfStockProduct> 
    </SiteStats> 
</SiteStats> 

您的一部开拓创新的代码很可能,如果你的XML看起来像上面

+0

这应该让你足够运行 –

+0

你能告诉我为什么我没有获得数据中的数据???虽然你的答案是真棒。 –

+1

看到我的编辑,希望是明确的 –

0

要允许解析,你也可能需要更改属性“工作xmlns“的标签

< HTML>通过noNSxml。如果这没有完成,那么它将解析不正确。