xml,json解析


 //4测试xml本地文件解析需要一个xml.的libxml2.dylib 和一个GData文件夹。

    //目标文件路径。

    NSString *xmlPath=[[[NSBundle mainBundle]bundlePath]stringByAppendingPathComponent:@"student.xml"];

    //目标文件内容。

    NSString *xmlContent=[NSString stringWithContentsOfFile:xmlPath encoding:NSUTF8StringEncodingerror:nil];

    NSLog(@"xmlContent===%@",xmlContent);

    //创建要解析的xml文档对象。

    GDataXMLDocument *doc=[[GDataXMLDocument alloc]initWithXMLString:xmlContent options:0 error:nil];

    //获取跟元素。

    GDataXMLElement *root=doc.rootElement;

    NSLog(@"root===%@",root);

    //从根元素获取元素。

    NSArray *docList=[root elementsForName:@"student"];//[root children]一样。

    NSLog(@"docList==%@",docList);

    //4获取第一个student元素

    GDataXMLElement *student1=[docList objectAtIndex:0];

    NSLog(@"student1==%@",student1);

    //5获取子元素

    NSArray *stuAry1=[student1 children];

    NSLog(@"stuAry1==%@",stuAry1);

    //获取每个子元素

    for (GDataXMLElement *e in stuAry1) {

        if ([e.name isEqualToString:@"name"]) {

            NSLog(@"ename====%@",e.stringValue);

        }

        GDataXMLNode *nod=[e attributeForName:@"first"];

        NSLog(@"nod==%@",[nod stringValue]);


2.json解析需要一个json的文件夹。

如果想按一下背景就可以取消自带的键盘,可以在uiview中的。touch事件中写一个[textField resignFirstResponse]就行了。

#import "ViewController.h"

#import "JSON.h"

@implementation ViewController

@synthesize url;

@synthesize urlImage;

@synthesize imageData;


- (void)viewDidLoad

{

    [super viewDidLoad];

    NSLog(@"%@",__FUNCTION__);

//    NSString *str=[NSString stringWithFormat:@"http://api.jiepang.com/v1/locations/search?lat=39.916&lon=116.393&count=2&source=100000"];

//    NSURL *url3=[NSURL URLWithString:str];

//    NSURLRequest *req=[NSURLRequest requestWithURL:url3];

//    [NSURLConnection connectionWithRequest:req delegate:self];

    

//    NSString *string=[NSString stringWithContentsOfFile:url encoding:NSUTF8StringEncoding error:nil];

//    NSLog(@"error =====%@",error);

//    NSLog(@"string=====%@",string);

//    //获取字典类型的数据。

//    NSDictionary *dic=[string JSONValue];

//    //statuses对应的value值。

//    NSArray *array=[dic valueForKey:@"statuses"];

//    //取数组的第一项。

//    NSDictionary *dicc=[array objectAtIndex:0];

//    //text对应的value

//    NSString *str=[dicc valueForKey:@"text"];

//    NSLog(@"str=====%@",str);  

    

}

- (IBAction)buttonClick:(id)sender {

    [url resignFirstResponder];

    //获取文本框地址

    NSString *urlString=self.url.text;

    //获取url对象。

    NSURL *url1=[NSURL URLWithString:urlString];

    //根据url对象,获取request对象。

    NSURLRequest *request=[NSURLRequest requestWithURL:url1cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:18];

//    NSURLRequest *re=[NSURLRequest requestWithURL:url1];

//    //发起同步连接返回给imageData

//    NSData *imageData=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

//    //给图形赋图像。

//    self.urlImage.image=[UIImage imageWithData:imageData];

    //发起异步连接

    [NSURLConnection connectionWithRequest:request delegate:self];  

}

//json 是另外一中数据格式组织形式,

//大括号代表字典,中括号,小括号代表数组,冒号左右分别为键和值。如name :"zhang"

#pragma mark=====异步链接的代理=====

//链接建立成功的代理。

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    NSLog(@"建立链接成功。");

//    self.imageData=[[NSMutableData alloc]init ];

//     NSLog(@"接受总数据的%d",length=[response expectedContentLength]);

//     NSLog(@"接受总数据的%lld",[response expectedContentLength]);

    self.imageData=[[NSMutableData alloc]init];

    

    

    

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    //将每次获取的数据添加到imageData中。

//    [self.imageData appendData:data];

//    float pr=[self.imageData length];

//    NSLog(@"百分比:===%%%6.2lf",pr/length*100);

//    NSLog(@"接受数据%d",data.length);

//    NSLog(@"接受数据%d",[data length]);

//   self.urlImage.image=[UIImage imageWithData:self.imageData];

    [self.imageData appendData:data];

    NSString *strData=[[NSString alloc]initWithData:self.imageData encoding:NSUTF8StringEncoding];

    

    NSMutableDictionary *dicJson=[strData JSONValue];

    NSLog(@"dicJson==%@",dicJson);

    NSArray *array=[dicJson valueForKey:@"items"];

    NSMutableDictionary *dic=[array objectAtIndex:0];

    NSMutableDictionary *dic1=[array objectAtIndex:1];

    NSString *stName=[dic valueForKey:@"name"];

    NSString *adr1=[dic valueForKey:@"addr"];

    NSString *stName2=[dic1 valueForKey:@"name"];

    NSString *adr2=[dic1 valueForKey:@"addr"];

    NSLog(@"stName======%@",stName);

    NSLog(@"adr1======%@",adr1);

    NSLog(@"stName2======%@",stName2);

    NSLog(@"adr2======%@",adr2);

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {

//    self.urlImage.image=[UIImage imageWithData:self.imageData];

}

- (void)viewDidUnload

{

    NSLog(@"%@",__FUNCTION__);

    [self setUrl:nil];

    [self setUrlImage:nil];

    [super viewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}

-(void)dealloc{

    NSLog(@"%@",__FUNCTION__);

    [url release];

    [urlImage release];

    [super dealloc];

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值