xml,json解析

xml,json解析

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

//目标文件路径。

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

//目标文件内容。

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

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

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

GDataXMLDocument *doc=[[GDataXMLDocumentalloc]initWithXMLString:xmlContent options:0error: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

{

[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=[NSURL URLWithString:urlString];

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

NSURLRequest *request=[NSURLRequestrequestWithURL:url1 cachePolicy:NSURLRequestReloadIgnoringLocalCacheDatatimeoutInterval:18];

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

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

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

// //给图形赋图像。

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

//发起异步连接

[NSURLConnectionconnectionWithRequest: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=[[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.imageData appendData:data];

NSString *strData=[[NSStringalloc]initWithData:self.imageDataencoding: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];

[selfsetUrlImage:nil];

[superviewDidUnload];

// Release any retained subviews of the main view.

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

}

-(void)dealloc{

NSLog(@"%@",__FUNCTION__);

[url release];

[urlImage release];

[super dealloc];

}

猜你喜欢

转载自zhangmingwei.iteye.com/blog/1877200