iOS开发之TBXMLParser解析

Home / iOS MrLee 2016-10-22 2706

TBXMLParser解析模式
NotesTBXMLParser.h文件代码如下
//  
//  NotesTBXMLParser.h  
//  TestXML  
//  
//  Created by choni on 14-5-16.  
//  Copyright (c) 2014年 choni. All rights reserved.  
//  
  
#import <Foundation/Foundation.h>  
  
@interface NotesTBXMLParser : NSObject  
  
//解析出的数据内部是字典类型  
  
@property (strong ,nonatomic) NSMutableArray * notes;  
  
// 开始解析  
-(void) start ;  
  
@end  

NotesTBXMLParser.m文件代码如下:
//  
//  NotesTBXMLParser.m  
//  TestXML  
//  
//  Created by choni on 14-5-16.  
//  Copyright (c) 2014年 choni. All rights reserved.  
//  
  
#import "NotesTBXMLParser.h"  
#import "TBXML.h"  
  
@implementation NotesTBXMLParser  
  
  
//开始解析  
-(void)start  
{  
    _notes = [NSMutableArray new];  
      
    TBXML* tbxml = [[TBXML alloc] initWithXMLFile:@"Notes.xml" error:nil];  
      
    TBXMLElement * root = tbxml.rootXMLElement;  
      
    // if root element is valid  
    if (root) {  
          
        TBXMLElement * noteElement = [TBXML childElementNamed:@"Note" parentElement:root];  
          
        while ( noteElement != nil) {  
              
            NSMutableDictionary *dict = [NSMutableDictionary new];  
              
            TBXMLElement *CDateElement = [TBXML childElementNamed:@"CDate" parentElement:noteElement];  
            if ( CDateElement != nil) {  
                NSString *CDate = [TBXML textForElement:CDateElement];  
                NSLog(@"CDate == %@",CDate);  
                [dict setValue:CDate forKey:@"CDate"];  
            }  
              
            TBXMLElement *ContentElement = [TBXML childElementNamed:@"Content" parentElement:noteElement];  
            if ( ContentElement != nil) {  
                NSString *Content = [TBXML textForElement:ContentElement];  
                [dict setValue:Content forKey:@"Content"];  
            }  
              
            TBXMLElement *UserIDElement = [TBXML childElementNamed:@"UserID" parentElement:noteElement];  
            if ( UserIDElement != nil) {  
                NSString *UserID = [TBXML textForElement:UserIDElement];  
                [dict setValue:UserID forKey:@"UserID"];  
            }  
              
            //获得ID属性  
            NSString *_id = [TBXML valueOfAttributeNamed:@"id" forElement:noteElement error:nil];  
            [dict setValue:_id forKey:@"id"];  
              
            [_notes addObject:dict];  
              
              
            noteElement = [TBXML nextSiblingNamed:@"Note" searchFromElement:noteElement];  
              
        }  
    }  
      
    NSLog(@"解析完成...");  
      
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadViewNotification" object:self.notes userInfo:nil];  
    self.notes = nil;  
      
}  
  
  
  
@end 

OK 搞定, 最后在 chonVIewController.m 文件中得 viewLoad方法中调用就好了 !
最后声明 :
TBXML解析xml 文档采用的是 DOM解析模式,通过上面的比较,发现他是非常好的解析框架,速度是所有xml中最快的 ,下面简单的介绍下如何使用
1.首先要到技术支持网站: http://www.tbxml.co.uk/TBXML/TBXML_Free.html 下载,下载完成后解压 出 TBXML-Headers 和TBXML-Code文件 并添加到工程
2.该框架不支持ARC 因此在使用该框架运行时 会报ARC 错误,这里就不吧 异常错误贴出来了 ,只提供解决的方法,需要修改工程目录中 TestXML-Prefix.pch 这个文件 ,在这个文件里 加上宏:如下图
#define ARC_ENABLED


3.由于TBXML依赖libz.dylib 库,还需要在工程中Framework中添加这个库,具体的添加方法 在这里就不说了, 完成以上3个步骤 在进行编译就可以啦!
今天先把xml 代码贴出来了,后续还有json数据解析,下午如果有空的话,搞个demo 在弄上来吧,希望对有得人有所帮助,因为我是新手,哈哈!刚学,献丑咯!

本文链接:https://www.it72.com/10952.htm

推荐阅读
最新回复 (0)
返回