问题
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>
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...
}
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);
}
}
I can get this value when i 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>"}
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.
回答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);
//}
}
EDIT:
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>
Your orignal code is likely to work if your XML looks something like above
回答2:
To allow the parsing you may also need to change the attribute "xmlns" in the tag
< html > by noNSxml. If this is not done then it will parse incorrectly.
来源:https://stackoverflow.com/questions/8986363/i-am-not-able-to-parse-xml-data-using-gdataxml