我无法使用GDataXML解析xml数据 [英] I am not able to parse xml data using GDataXML
问题描述
起初,我在.h文件中导入了#import "GDataXMLNode.h"
.现在这是我的XML
,我必须使用GDataXML
解析器进行解析.
At first i have imported #import "GDataXMLNode.h"
in .h file. Now this is my XML
which i have to parse using GDataXML
parser.
<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数据.
Now, one thing to notice is that my this xml is coming from web. So I have used NSUrlConnection
delegate to retrieve xml data.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"Response: %@",responseString);
// other stuff here...
}
在这里,我得到了responseString
,然后使用以下代码对其进行了解析.但是我无法解析它.
Here I get the responseString
and then I parse it using the following code. But I'm not able to parse it.
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
数组中获取任何数据.
But i use NSLog(@"%@",getData);
I do not get any data in getData
array.
有人可以告诉我问题出在哪里吗?预先谢谢你.
Can anybody tell me where is the problem? Thank you in advance.
推荐答案
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"已经是根元素,因此,当您执行这些代码时,您要尝试的是在其中查找"SiteStats" "SideStats"
NSArray *getData = [[xmlDocument rootElement] elementsForName:@"SiteStats"];
in this line, you're trying to retrieve wrong element, element "SiteStats" is already the root element, so when you do that bit of code, what you're trying to do is looking for "SiteStats" inside of "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与上面类似,您的原始代码可能会起作用
Your orignal code is likely to work if your XML looks something like above
这篇关于我无法使用GDataXML解析xml数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!