XML JSON解析--基本功能

一,json的解析

json文件:

{"code": "cn","cities":   
[{"name": "保定", "lat": 38849998, "lon": 115569999},
{"name": "驻马店", "lat": 33000000, "lon": 114019996}]
}

定义一个可变数组存放每条数据

@property (nonatomic ,strong) NSMutableArray *cityList;

self.cityList = [NSMutableArray array];//首先别忘了初始化

NSString *paths=[[NSBundle mainBundle]resourcePath];//获取文件所在的文件夹路径
    NSString *path=[paths stringByAppendingPathComponent:@"citys.txt"];//获取文件全路径
    NSData *data=[NSData dataWithContentsOfFile:path];//读取文件到data

//将data数据转化成 json格式的数据到字典中
    NSDictionary *parser=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

将字典存入数组:  [self.cityList addObjectsFromArray:[parser objectForKey:@"cities"]];

最后读取:

NSDictionary *dic = self.cityList[indexPath.row];
    NSString *name= [dic objectForKey:@"name"];
    NSString *latitude=[dic objectForKey:@"lat"];
    NSString *longitude=[dic objectForKey:@"lon"];

2,XML的解析,基本和json的解析一样

xml文件内容格式:

<?xml version="1.0"?>
<xml_api_reply version="1">
    <cities>
        <city>
            <name data="保定"/>
            <latitude_e6 data="38849998"/>
            <longitude_e6 data="115569999"/>
        </city>
        <city default="true" >
            <name data="北京"/>
            <latitude_e6 data="39930000"/>
            <longitude_e6 data="116279998"/>
        </city>
 </cities>
</xml_api_reply>

如json一样的步骤:

self.cityList=[[NSMutableArray alloc]init];//定义数组存放字典:name,latitude_e6,longitude_e6
    
    NSString *paths=[[NSBundle mainBundle]resourcePath];
    NSString *path=[paths stringByAppendingPathComponent:@"citys.xml"];
    //从路径获取文件内容
    NSData *data=[NSData dataWithContentsOfFile:path];
    GDataXMLDocument *doc=[[GDataXMLDocument alloc]initWithData:data options:0 error:Nil];
    //得到根节点
    GDataXMLElement *root=doc.rootElement;
    GDataXMLElement *secondLevalRoot=[[root elementsForName:@"cities"]lastObject];
    NSArray *cities=[secondLevalRoot elementsForName:@"city"];
    for (GDataXMLElement * item in cities) {
         NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        
        for (GDataXMLElement *detail in item.children) {
              [dic setObject:[detail attributeForName:@"data"].stringValue forKey:detail.name];
        }
         [self.cityList addObject:dic];
    }
    
最后读取:

NSDictionary *dic = self.cityList[indexPath.row];
    NSString *name= [dic objectForKey:@"name"];
    NSString *latitude=[dic objectForKey:@"latitude_e6"];
    NSString *longitude=[dic objectForKey:@"longitude_e6"];

三,对网络数据进行读取

-(void)didClickParse:(id)sender
{
NSString *urlstring=@"http://124.205.147.26/student/class_12/team_learn/lichanghong.php";
    NSURL *url=[NSURL URLWithString:urlstring];//此处网络地址可以换成超链接
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:Nil error:Nil];
    //NSString *content=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    //NSLog(@"%@",content);
    
    //解析
    NSArray *parser=[NSJSONSerialization JSONObjectWithData:data options:0 error:Nil];
    NSLog(@"%@",parser);
    
}

四,对字符串进行解析

首先需要引入协议NSXMLParserDelegate

-(void)didClickParser:(id)sender
{
    NSLog(@"%@",[NSDate date].description);
    NSString *xml=@"<root>    <image>http://www,baidu.com/1.jpg</image> <image>http://www,baidu.com/2.jpg</image>     <image>http://www,baidu.com/3.jpg</image> </root>";
    //NSXMLParser对象进行解析xml结构的字符串
    //dataUsingEncoding使用一种编码方式将字符串转换成二进制数据data对象
    
    NSXMLParser *parser=[[NSXMLParser alloc]initWithData:[xml dataUsingEncoding:NSUTF8StringEncoding]];
    //NSXMLParser的解析方式得到的返回结果是通过代理对象返回的
    NSLog(@"%@",parser);
    parser.delegate=self;
    
    [parser parse];
    
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
      NSLog(@"%@",string);
}

上一篇:JVM(2)——GC算法和收集器


下一篇:【AC大牛陈鸿的ACM总结贴】【ID AekdyCoin】人家当初也一样是菜鸟