ios-使用TBXML解析XML文件
2014-08-30 12:56
288 查看
ios平台除了可以使用NSXML解析XML文件外,还有TBXML框架也可以解析XML文件,TBXML采用DOM模式。
TBXML框架可以在网络上下载,网络地址为:https://github.com/71squared/TBXML ,下载完成并解压后,将TBXML-Headers和TBXML-Code文件夹拷贝到工程中,由于TBXML依赖libz.dylib库,还需要在工程Framework中添加这个库。(注:如果工程使用ARC,在工程的.pch文件中添加宏定义:#define
ARC_ENABLED)
至此,TBXML的介绍已经完毕,效果如下:
TBXML框架可以在网络上下载,网络地址为:https://github.com/71squared/TBXML ,下载完成并解压后,将TBXML-Headers和TBXML-Code文件夹拷贝到工程中,由于TBXML依赖libz.dylib库,还需要在工程Framework中添加这个库。(注:如果工程使用ARC,在工程的.pch文件中添加宏定义:#define
ARC_ENABLED)
1、例子Notes.xml:
<?xml version="1.0" encoding="UTF-8"?> <Notes> <Note id="1"> <CDate>2012-12-21</CDate> <Content>iosBOOK_1</Content> <UserID>tony</UserID> </Note> <Note id="2"> <CDate>2012-12-22</CDate> <Content>iosBOOK_2</Content> <UserID>tony</UserID> </Note> <Note id="3"> <CDate>2012-12-23</CDate> <Content>iosBOOK_3</Content> <UserID>tony</UserID> </Note> <Note id="4"> <CDate>2012-12-24</CDate> <Content>iosBOOK_4</Content> <UserID>tony</UserID> </Note> <Note id="5"> <CDate>2012-12-25</CDate> <Content>iosBOOK_5</Content> <UserID>tony</UserID> </Note> <Note id="6"> <CDate>2012-12-26</CDate> <Content>iosBOOK_6</Content> <UserID>tony</UserID> </Note> </Notes>
2、编写专门解析XML文件的类(NotesTBXMLParser):
- (void)start { _notes = [[[NSMutableArray alloc] init] autorelease]; TBXML *tbxml = [[TBXML alloc] initWithXMLFile:@"Notes.xml" error:nil]; TBXMLElement *root = tbxml.rootXMLElement; if (root) { TBXMLElement *noteElement = [TBXML childElementNamed:@"Note" parentElement:root]; while (noteElement != nil) { NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; TBXMLElement *CDateElement = [TBXML childElementNamed:@"CDate" parentElement:noteElement]; if (CDateElement != nil) { NSString *CDate = [TBXML textForElement:CDateElement]; [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"]; } NSString *_id = [TBXML valueOfAttributeNamed:@"id" forElement:noteElement]; [dict setValue:_id forKey:@"id"]; [_notes addObject:dict]; noteElement = [TBXML nextSiblingNamed:@"Note" searchFromElement:noteElement]; [dict release]; } NSLog(@"解析完成!"); // 使用KVO模式,通知中心机制 [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadXMLData" object:_notes userInfo:nil]; [_notes removeAllObjects]; _notes = nil; } }
3、实际使用:
- (void)initObserver { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getXMLData:) name:@"reloadXMLData" object:nil]; // 开始解析xml文件 NotesTBXMLParser *parser = [[NotesTBXMLParser alloc] init]; [parser start]; [parser release]; } - (void)getXMLData:(NSNotification *)notification { NSMutableArray *xmlData = [notification object]; NSLog(@"xmlData:%@",xmlData); }
至此,TBXML的介绍已经完毕,效果如下:
2014-08-30 12:57:46.031 test71[7145:60b] 解析完成! 2014-08-30 12:57:46.033 test71[7145:60b] xmlData:( { CDate = "2012-12-21"; Content = "iosBOOK_1"; UserID = tony; id = 1; }, { CDate = "2012-12-22"; Content = "iosBOOK_2"; UserID = tony; id = 2; }, { CDate = "2012-12-23"; Content = "iosBOOK_3"; UserID = tony; id = 3; }, { CDate = "2012-12-24"; Content = "iosBOOK_4"; UserID = tony; id = 4; }, { CDate = "2012-12-25"; Content = "iosBOOK_5"; UserID = tony; id = 5; }, { CDate = "2012-12-26"; Content = "iosBOOK_6"; UserID = tony; id = 6; } )
相关文章推荐
- ios-使用NSXMLParser解析XML文件
- iOS_使用NSXMLParser解析本地XML文件
- iOS 使用ASIHttpRequest和GData解析xml网络文件
- IOS 解析XML文档 使用TBXML
- iOS开发中 使用XML和JSON对本地文件进行解析
- iOS:使用第三方DOM解析XML文件
- 使用MSXML解析XML文件
- 使用JDOM创建和解析XML文件
- 使用Dom4j创建与解析XML文件
- android 使用 pull 解析xml文件
- VC中使用CMarkup解析xml文件
- 在JSP开发中使用jdom解析临时存放数据的XML文件
- 使用NSXMLParser解析xml文件
- 使用Digester解析XML文件和JAVA对象的映射
- 使用Digester解析XML配置文件
- ios下TBXML解析xml
- 使用Pull解析器解析XML文件和写XML
- 使用MSXML解析XML文件
- [Java学习]使用digester 解析xml配置文件
- 在vc中使用MSXML解析xml文件