xml,json解析


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

//目标文件路径。

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

//目标文件内容。

NSString*xmlContent=[NSStringstringWithContentsOfFile:xmlPathencoding:NSUTF8StringEncodingerror:nil];

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

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

GDataXMLDocument*doc=[[GDataXMLDocumentalloc]initWithXMLString:xmlContentoptions:0error:nil];

//获取跟元素。

GDataXMLElement*root=doc.rootElement;

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

//从根元素获取元素。

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

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

//4获取第一个student元素

GDataXMLElement*student1=[docListobjectAtIndex:0];

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

//5获取子元素

NSArray*stuAry1=[student1children];

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

//获取每个子元素

for(GDataXMLElement*einstuAry1) {

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

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

}

GDataXMLNode*nod=[eattributeForName:@"first"];

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


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

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

#import"ViewController.h"

#import"JSON.h"

@implementationViewController

@synthesizeurl;

@synthesizeurlImage;

@synthesizeimageData;


- (void)viewDidLoad

{

[superviewDidLoad];

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 {

[urlresignFirstResponder];

//获取文本框地址

NSString*urlString=self.url.text;

//获取url对象。

NSURL*url1=[NSURLURLWithString:urlString];

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

NSURLRequest*request=[NSURLRequestrequestWithURL:url1cachePolicy:NSURLRequestReloadIgnoringLocalCacheDatatimeoutInterval:18];

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

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

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

// //给图形赋图像。

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

//发起异步连接

[NSURLConnectionconnectionWithRequest:requestdelegate: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=[[NSMutableDataalloc]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.imageDataappendData:data];

NSString*strData=[[NSStringalloc]initWithData:self.imageDataencoding:NSUTF8StringEncoding];

NSMutableDictionary*dicJson=[strDataJSONValue];

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

NSArray*array=[dicJsonvalueForKey:@"items"];

NSMutableDictionary*dic=[arrayobjectAtIndex:0];

NSMutableDictionary*dic1=[arrayobjectAtIndex:1];

NSString*stName=[dicvalueForKey:@"name"];

NSString*adr1=[dicvalueForKey:@"addr"];

NSString*stName2=[dic1valueForKey:@"name"];

NSString*adr2=[dic1valueForKey:@"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__);

[selfsetUrl:nil];

[selfsetUrlImage:nil];

[superviewDidUnload];

// Release any retained subviews of the main view.

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

}

-(void)dealloc{

NSLog(@"%@",__FUNCTION__);

[urlrelease];

[urlImagerelease];

[superdealloc];

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值