解析(xml,json)

第一步;导入解析文件

配置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);
    }
    




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值