简单的GDataXML 解析例子

 +(NSDictionary *) XmlAnalysisWithString:(NSString *) URLString

{

NSError* error;

NSMutableDictionary * dataDic = [NSMutableDictionary dictionary];

//拿出xml文件内容

NSURL * myURL = [NSURL URLWithString:URLString];

NSString* fileContent = [[NSString alloc] initWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:&error];

GDataXMLDocument* document = [[GDataXMLDocument alloc] initWithXMLString:fileContent options:1 error:&error];

 

if (error) {

NSLog(@"%@", [error localizedDescription]);

} else {

//取出xml的根节点

GDataXMLElement* rootElement = [document rootElement];

GDataXMLElement * testElement = nil;

GDataXMLElement * childTestElement = nil;

//取出根节点的所有孩子节点

for (int i = 0; i < [[rootElement children] count]; i++) {

testElement =  [[rootElement children]objectAtIndex:i];

//获取channelname

NSString * titleContent = [[[testElement elementsForName:@"channelname"]objectAtIndex:0] stringValue];

NSArray * children = [testElement children];

NSMutableArray * allData = [[NSMutableArray alloc]init];

//从第2个节点开始都是item ,第一个节点是每一个的channelname

for (int j = 1; j < [children count]; j++)

{

NSMutableDictionary * everyDic = [[NSMutableDictionary alloc]init];

childTestElement = [children objectAtIndex:j];

//获得每一个item下的有用信息

NSString * topicContent = [[[childTestElement elementsForName:@"topic"]objectAtIndex:0] stringValue] ;

NSString * summaryContent = [[[childTestElement elementsForName:@"summary"]objectAtIndex:0] stringValue] ;

NSString * picContent = [[[childTestElement elementsForName:@"pic"]objectAtIndex:0] stringValue] ;

NSString * infoidContent = [[[childTestElement elementsForName:@"infoid"]objectAtIndex:0] stringValue] ;

 

[everyDic setObject:topicContent forKey:@"topic"];

[everyDic setObject:summaryContent forKey:@"summary"];

[everyDic setObject:picContent forKey:@"pic"];

[everyDic setObject:infoidContent forKey:@"infoid"];

 

//把每一个item加进每一个channel的数组中

[allData addObject:everyDic];

}

//根据channel的名字存储数组

[dataDic setObject:allData forKey:titleContent];

}

}

return dataDic;

}

 

你可能感兴趣的:(error,的)