XML 解析:
//配置GData库
//配置XML库(配置完才能使用)
//(1)添加头文件搜索路径
// Header Search Paths-> /usr/include/libxml2
//(2)添加二进制库
// Link library -> libxml2.dylib
//(3)源文件添加编译选项
// -fno-objc-arc
//(4)添加头文件
// #import "GDataXMLNode.h"
#import “GDataXMLNode.h"
// xml网络数据解析
- (void)xmlUse
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:_urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:responseObject options:0 error:nil];
NSLog(@"%@",doc);
NSArray *array = [doc nodesForXPath:@"//newslist" error:nil];
for(GDataXMLElement *e in array)
{
NSString *id = [[[e elementsForName:@"id"] firstObject] stringValue];
NSString *price = [[[e elementsForName:@"price"] firstObject] stringValue];
NSString *name = [[[e elementsForName:@"name"] firstObject] firstObject];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
}
- (void)xmlBasicUse
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"xml.txt" ofType:nil];
NSData *data = [[NSData alloc]initWithContentsOfFile:path];
GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:data options:0 error:nil];
NSArray *array = [doc nodesForXPath:@"//promotion" error:nil];
for(GDataXMLElement *e in array)
{
NSLog(@"name = %@",e.name);
NSString *id = [[[e elementsForName:@"id"] firstObject] stringValue];
NSString *price = [[[e elementsForName:@"price"] firstObject] stringValue];
NSString *name = [[[e elementsForName:@"name"] firstObject] firstObject];
NSLog(@"%@ - %@ - %@",id,name,price);
PromotionModel *model = [[PromotionModel alloc]init];
model.id = id;
model.name = name;
model.price = price;
}
}