码迷,mamicode.com
首页 > 其他好文 > 详细

XML解析-第三方和系统方法

时间:2016-05-12 13:45:07      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

一、DOM树状解析  一个个节点解析

GData的抓准备工作

     1.导入动态库: libxml2.dylib

     2.buid setting 里面搜索 header search paths ,添加//usr/include/libxm2

     3.buid setting 里面搜索 other linker flags ,添加 -lxml2

     4.GData JSONKit 都是MRC下的,如果想在ARC下使用 ,加上一句修饰 -font object ******

//1.获取XML路径
    
    NSString *fielPath = [[NSBundle mainBundle]pathForResource:@"Student.xml" ofType:nil];
    
    //2.将XML文件转成字符串
    NSError *error = nil ;
    NSString *xmlString = [[NSString alloc]initWithContentsOfFile:fielPath encoding:NSUTF8StringEncoding error:&error];
    
    //3.讲XML字符串转化为XML类型的数据
    GDataXMLDocument *document = [[GDataXMLDocument alloc]initWithXMLString:xmlString options:0 error:&error];
    
    //4.获取根节点
    GDataXMLElement *rootElement = [document rootElement];
    
    //5.根据标签节点名字来取(一层层取)
    NSArray *studentArray = [rootElement elementsForName:@"student"];
    
    self.dataArray = [NSMutableArray array];
    
    for (int i = 0; i < studentArray.count; i ++) {
        
        Student *student = [[Student alloc]init];
        
        GDataXMLElement *element = studentArray[i];
        
        NSArray *nameArray = [element elementsForName:@"name"];
        
        student.name = [nameArray[0] stringValue];
        
        NSArray *ageArray = [element elementsForName:@"age"];
        student.age = [ageArray[0] stringValue];
        
        NSArray *sexArray = [element elementsForName:@"sex"];
        student.sex = [sexArray[0] stringValue];
        
        [self.dataArray addObject:student];
    }



二、系统自带的方法-SAX

#import "MainViewController.h"
#import "Student.h"
@interface MainViewController ()<NSXMLParserDelegate>

@property (nonatomic,retain) NSMutableArray *dataArray ;

//记录当前解析的节点标签的名字
@property (nonatomic,retain) NSString *currenElementName ;

@property (nonatomic,retain) Student *student ;

@end

@implementation MainViewController

-(void)dealloc{
    [_currenElementName release];
    [_student release];
    [_dataArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    //SAX
    
    //1.获取路径
    
    NSString *fielPath = [[NSBundle mainBundle]pathForResource:@"Student.xml" ofType:nil];
    
    //2.转化为xml为二进制数据
    NSData *data = [NSData dataWithContentsOfFile:fielPath];
    
    //3.利用系统的SAX 解析
    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
    
    //4.指定代理  使用代理方法
    parser.delegate = self ;
    
    self.dataArray = [NSMutableArray array];
    
    //开始解析数据
    [parser parse];
    
    
    
    
    
}


-(void)parserDidStartDocument:(NSXMLParser *)parser{
    
    NSLog(@"开始解析");
    
//    self.currenElementName = nil ;
}



//开始解析某个标签
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    
    NSLog(@"开始解析标签%@",elementName);
    
    self.currenElementName = elementName ;
    
    
    
}

//取值方法
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    
    NSLog(@"取到的值:%@",string);
    
    if ([self.currenElementName isEqualToString:@"student"]) {
        self.student = [[Student alloc]init];
        
    }
    
    if ([self.currenElementName isEqualToString:@"name"] &&
        self.student.name == nil) {
        self.student.name = string ;
    }
    
    if ([self.currenElementName isEqualToString:@"age"] &&
        self.student.age == nil) {
        self.student.age = string ;
    }
    
    if ([self.currenElementName isEqualToString:@"sex"] &&
        self.student.sex == nil) {
        self.student.sex = string ;
    }
    
    
}

//结束某个标签
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"student"]) {
        
         [self.dataArray addObject:self.student];
        
        self.student = nil ;
    }
    
    NSLog(@"结束节点 %@",elementName);
    
}

//结束整个文件解析
-(void)parserDidEndDocument:(NSXMLParser *)parser{
    
    NSLog(@"解析完成");
}



XML解析-第三方和系统方法

标签:

原文地址:http://blog.csdn.net/y_csdnblog_xx/article/details/51365042

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!