第一步;导入解析文件
配置XML :header search 添加 /usr/include/libxml2 如果报错前面加$(SDKROOT)
导入libxml2.dylib 框架
Other linker flags 添加:-lxml2
//创建XML 读取本地内容
NSString *path=[[NSBundle mainBundle] pathForResource:@"new" ofType:@"xml"];
//读到字符串里面
NSString *newStr=[NSString stringWithContentsOfFile:path encoding:4 error:nil];
//初始化一个DOCMEN的 文档
GDataXMLDocument *doc=[[GDataXMLDocument alloc]initWithXMLString:newStr options:0 error:nil];
//找根节点 根元素
GDataXMLElement *root=doc.rootElement;
//找news new很多 所以 仍到一个数组里面
NSArray *newElements=[root elementsForName:@"new"];
for (GDataXMLElement *e in newElements)
{
//提取元素节点 返回的是一个数组
GDataXMLElement *titleEle=[[e elementsForName:@"title"]objectAtIndex:0];
GDataXMLElement *subEle=[[e elementsForName:@"subtitle"]objectAtIndex:0];
GDataXMLElement *imgEle=[[e elementsForName:@"img"]objectAtIndex:0];
//在元素节点提取 文本
NSString *title=[titleEle stringValue];
NSString *subTitle=[subEle stringValue];
NSString *img=[imgEle stringValue];
Newinfo *new=[[Newinfo alloc]initWithTitle:title SubTitle:subTitle Img:img];
NSLog(@"title ==%@/nsubtitle =%@ /nimg==%@ ",title,subTitle,img);
//创建JSON 本地内容
//JSON
/*
//网络
NSString *ur=[NSString stringWithFormat:@"http://best50.cn:8080/php/new_iphone.php?req=2&id=13"];
NSURL *url=[NSURL URLWithString:ur];
NSString *str=[NSString stringWithContentsOfURL:url encoding:4 error:nil];
*/
NSString *path=[[NSBundle mainBundle] pathForResource:@"stu" ofType:@"json"];
//读到字符串里面
NSString *stuStr=[NSString stringWithContentsOfFile:path encoding:4 error:nil];
//解析
NSArray *stuArray=[stuStr JSONValue];
NSLog(@"%@",stuArray);
//遍历
for (NSDictionary *d in stuArray)
{
NSString *name=[d valueForKey:@"name"];
NSLog(@"name==%@",name);
}
解析(xml,json)
最新推荐文章于 2024-04-18 19:42:07 发布