`
zjjzmw1
  • 浏览: 1354695 次
  • 性别: Icon_minigender_1
  • 来自: 开封
社区版块
存档分类
最新评论

xml从零到精通------自己的总结

    博客分类:
  • iOS
阅读更多


1.可扩展标记语言extensible markup language;XML

2.用于标记电子文件使其具有结构性的标记语言,可以用来标记数据、定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言。

3,数据库提供了更强有力的数据存储和分析能力,例如:数据索引、排序、查找、相关一致性等,XML仅仅是存储数据。

4.XMLHTML的设计区别是:XML的核心是数据,其重点是数据的内容。而HTML 被设计用来显示数据,其重点是数据的显示。

5.XMLHTML语法区别:HTML的标记不是所有的都需要成对出现,XML则要求所有的标记必须成对出现;HTML标记不区分大小写,XML 大小敏感,即区分大小写。

 

结合

  XML的简单使其易于在任何应用程序中读写数据,这使XML很快成为数据交换的唯一公共语言,虽然不同的应用软件也支持其它的数据交换格式,但不久之后他们都将支持XML,那就意味着程序可以更容易的与Windows,Mac OS,Linux以及其他平台下产生的信息结合,然后可以很容易加载XML数据到程序中并分析他,并以XML格式输出结果。

  XML去掉了之前令许多开发人员头疼的SGML(标准通用标记语言)的随意语法。在XML中,采用了如下的语法:

  1 任何的起始标签都必须有一个结束标签

  2 可以采用另一种简化语法,可以在一个标签中同时表示起始和结束标签。这种语法是在大于符号之前紧跟一个斜线(/),例如<tag/ >XML解析器会将其翻译成<tag></tag>

  3 标签必须按合适的顺序进行嵌套,所以结束标签必须按镜像顺序匹配起始标签,例如this is a samplestring。这好比是将起始和结束标签看作是数学中的左右括号:在没有关闭所有的内部括号之前,是不能关闭外面的括号的。

  4 所有的特性都必须有值。

  5 所有的特性都必须在值的周围加上双引号。

  这些规则使得开发一个XML解析器要简便得多,而且也除去了解析SGML中花在判断何时何地应用那些奇怪语法规则上的工作。仅仅在XML出现后的前六年就衍生出多种不同的语言,包括MathMLSVGRDFRSSSOAPXSLTXSL-FO,而同时也将HTML改进为XHTML

下面是ios中xml的解析。

 

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

    //目标文件路径。

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

    //目标文件内容。

    NSString *xmlContent=[NSString stringWithContentsOfFile:xmlPath encoding:NSUTF8StringEncoding error: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]);

 

下面附加一些json的解析。

 

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:url1 cachePolicy: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];

}

 

2
3
分享到:
评论
1 楼 zhukewen_java 2012-12-13  
好牛B呀

相关推荐

Global site tag (gtag.js) - Google Analytics