zoukankan      html  css  js  c++  java
  • XML_DOM和XML_SAX数据解析(初学)

    方法一:XML_DOMXML_DOM数据解析

     1 //DOM树状解析  一个个节点解析
     2     
     3     /*
     4      GData的抓准备工作
     5      1.导入动态库: libxml2.dylib
     6      2.在buid setting 里面搜索 header search paths ,添加//usr/include/libxm2
     7      3.在buid setting 里面搜索 other linker flags ,添加 -lxml2
     8      4.GData 和 JSONKit 都是MRC下的,如果想在ARC下使用 ,加上一句修饰 -font object ******
     9      */
    10     
    11     
    12     // XML DOM解析
    13     
    14     //1.获取XML路径
    15     
    16     NSString *fielPath = [[NSBundle mainBundle]pathForResource:@"Student.xml" ofType:nil];
    17     
    18     //2.将XML文件转成字符串
    19     NSError *error = nil ;
    20     NSString *xmlString = [[NSString alloc]initWithContentsOfFile:fielPath encoding:NSUTF8StringEncoding error:&error];
    21     
    22     //3.讲XML字符串转化为XML类型的数据
    23     GDataXMLDocument *document = [[GDataXMLDocument alloc]initWithXMLString:xmlString options:0 error:&error];
    24     
    25     //4.获取根节点
    26     GDataXMLElement *rootElement = [document rootElement];
    27     
    28     //5.根据标签节点名字来取(一层层取)
    29     NSArray *studentArray = [rootElement elementsForName:@"student"];
    30     
    31     self.dataArray = [NSMutableArray array];
    32     
    33     for (int i = 0; i < studentArray.count; i ++) {
    34         
    35         Student *student = [[Student alloc]init];
    36         
    37         GDataXMLElement *element = studentArray[i];
    38         
    39         NSArray *nameArray = [element elementsForName:@"name"];
    40         
    41         student.name = [nameArray[0] stringValue];
    42         
    43         NSArray *ageArray = [element elementsForName:@"age"];
    44         student.age = [ageArray[0] stringValue];
    45         
    46         NSArray *sexArray = [element elementsForName:@"sex"];
    47         student.sex = [sexArray[0] stringValue];
    48         
    49         [self.dataArray addObject:student];
    50     }
    51     
    52     
    53     

    方法二:XML_SAX数据解析

      1 #import "MainViewController.h"
      2 #import "Student.h"
      3 @interface MainViewController ()<NSXMLParserDelegate>
      4 
      5 @property (nonatomic,retain) NSMutableArray *dataArray ;
      6 
      7 //记录当前解析的节点标签的名字
      8 @property (nonatomic,retain) NSString *currenElementName ;
      9 
     10 @property (nonatomic,retain) Student *student ;
     11 
     12 @end
     13 
     14 @implementation MainViewController
     15 
     16 -(void)dealloc{
     17     [_currenElementName release];
     18     [_student release];
     19     [_dataArray release];
     20     [super dealloc];
     21 }
     22 
     23 - (void)viewDidLoad {
     24     [super viewDidLoad];
     25     // Do any additional setup after loading the view.
     26     
     27     
     28     //SAX
     29     
     30     //1.获取路径
     31     
     32     NSString *fielPath = [[NSBundle mainBundle]pathForResource:@"Student.xml" ofType:nil];
     33     
     34     //2.转化为xml为二进制数据
     35     NSData *data = [NSData dataWithContentsOfFile:fielPath];
     36     
     37     //3.利用系统的SAX 解析
     38     NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
     39     
     40     //4.指定代理  使用代理方法
     41     parser.delegate = self ;
     42     
     43     self.dataArray = [NSMutableArray array];
     44     
     45     //开始解析数据
     46     [parser parse];
     47     
     48     
     49     
     50     
     51     
     52 }
     53 
     54 
     55 -(void)parserDidStartDocument:(NSXMLParser *)parser{
     56     
     57     NSLog(@"开始解析");
     58     
     59 //    self.currenElementName = nil ;
     60 }
     61 
     62 
     63 
     64 //开始解析某个标签
     65 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
     66     
     67     NSLog(@"开始解析标签%@",elementName);
     68     
     69     self.currenElementName = elementName ;
     70     
     71     
     72     
     73 }
     74 
     75 //取值方法
     76 -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
     77 {
     78     
     79     NSLog(@"取到的值:%@",string);
     80     
     81     if ([self.currenElementName isEqualToString:@"student"]) {
     82         self.student = [[Student alloc]init];
     83         
     84     }
     85     
     86     if ([self.currenElementName isEqualToString:@"name"] &&
     87         self.student.name == nil) {
     88         self.student.name = string ;
     89     }
     90     
     91     if ([self.currenElementName isEqualToString:@"age"] &&
     92         self.student.age == nil) {
     93         self.student.age = string ;
     94     }
     95     
     96     if ([self.currenElementName isEqualToString:@"sex"] &&
     97         self.student.sex == nil) {
     98         self.student.sex = string ;
     99     }
    100     
    101     
    102 }
    103 
    104 //结束某个标签
    105 -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    106 {
    107     if ([elementName isEqualToString:@"student"]) {
    108         
    109          [self.dataArray addObject:self.student];
    110         
    111         self.student = nil ;
    112     }
    113     
    114     NSLog(@"结束节点 %@",elementName);
    115     
    116 }
    117 
    118 //结束整个文件解析
    119 -(void)parserDidEndDocument:(NSXMLParser *)parser{
    120     
    121     NSLog(@"解析完成");
    122 }
  • 相关阅读:
    Spark Scala当中reduceByKey(_+_) reduceByKey((x,y) => x+y)的用法
    Spark和Scala当中的collect方法的用法和例子
    Win7 Eclipse 搭建spark java1.8(lambda)环境:WordCount helloworld例子
    System memory 259522560 must be at least 4.718592
    Win7 Eclipse 搭建spark java1.8环境:WordCount helloworld例子
    Win7 Eclipse 搭建spark java1.8编译环境,JavaRDD的helloworld例子
    indows Eclipse Scala编写WordCount程序
    Spark Scala当中reduceByKey的用法
    Spark Scala当中reduce的用法和例子
    Scala当中parallelize并行化的用法
  • 原文地址:https://www.cnblogs.com/yyxblogs/p/4878843.html
Copyright © 2011-2022 走看看