现在的位置: 首页 > 综合 > 正文

OS中XML解析 (一) TBXML (实例:打印xml内容及存储到数组)

2013年02月01日 ⁄ 综合 ⁄ 共 9178字 ⁄ 字号 评论关闭

 

 

 

在时间上TBXML占优,libxml2支持了边下载边解析。

 

来源:http://www.codeios.com/forum.php?mod=viewthread&tid=9880&highlight=xml

 

 

解析 XML 通常有两种方式,DOM 和 SAX:

 

DOM解析XML时,读入整个XML文档并构建一个驻留内存的树结构(节点树),通过遍历树结构可以检索任意XML节点,读取它的属性和值。而且通常情况下,可以借助XPath,直接查询XML节点。

 

SAX解析XML,是基于事件通知的模式,一边读取XML文档一边处理,不必等整个文档加载完之后才采取操作,当在读取解析过程中遇到需要处理的对象,会发出通知对其进行处理。

 

一般在iOS平台下,比较常用的XML解析类库有如下几种:

 

NSXMLParser这是一个SAX方式解析XML的类库,默认包含在iOS SDK中,使用也比较简单。

 

libxml2是一套默认包含在iOS SDK中的开源类库,它是基于C语言的API,所以使用起来可能不如NSXML方便。这套类库同时支持DOM和SAX解析,libxml2的SAX解析方式还是非常酷的,因为它可以边读取边解析,尤其是在从网上下载一个很大的XML文件,就可以一边下载一边对已经下载好的内容进行解析,极大的提高解析效率。

 

TBXML这是一套轻量级的DOM方式的XML解析类库,有很好的性能和低内存占用,不过它不对XML格式进行校验,不支持XPath,并且只支持解析,不支持对XML进行修改。

 

TouchXML这也是一套DOM方式的XML解析类库,支持XPath,不支持XML的修改。

 

KissXML这是一套基于TouchXML的XML解析类库,和TouchXML相比,支持了XML的修改。

 

TinyXML这是一套小巧的基于C语言的DOM方式进行XML解析的类库,支持对XML的读取和修改,不直接支持XPath,需要借助另一个相关的类库TinyXPath才可以支持XPath。

 

GDataXML这是一套Google开发的DOM方式XML解析类库,支持读取和修改XML文档,支持XPath方式查询。

 

那么对于如何在项目中选择合适的XML解析类库呢?网上已经有人对这几款XML类库做过分析和对比,可参考《How To Choose The Best XML Parser for Your iPhone Project》 一文,基本比较准确和客观,文中建议:

 

如果是读取很小的XML文档,性能基本上没有什么差别,不过从调用的方便性来说,建议使用TouchXML、KissXML或GDataXML

 

如果是需要读取和修改XML文档,建议使用KissXML或GDataXML

 

如果需要读取非常大的XML文档,则建议使用libxml2或TBXML

 

如果你不想去调用第三方类库,那么使用NSXML也可以

 

 

TBXML

 

1. 解压TBXML.zip后,将包含TBXML.h, TBXML.m, NSDataAdditions.h和NSDataAddtions.m四个文件的文件夹拖到项目中

2. 添加对libz.dylib类库的应用 (项目=>Build Phases=>Link Binary With Libraries=>+=>libz.dylib=>add)

 

在需要调用TBXML的代码文件头部,加入:

C代码  收藏代码
  1. #import "TBXML.h"  

 

来源: http://www.61ic.com/Mobile/iPhone/201108/36543.html

 

 

使用说明:

 

.h

C代码  收藏代码
  1. #import <UIKit/UIKit.h>  
  2. #import "TBXML.h"  
  3.   
  4. @interface SecondViewController : UIViewController  
  5. {  
  6.     TBXML *tbxml;  
  7.     NSMutableArray *articles;  
  8. }  
  9.   
  10. - (void) loadURL;  
  11. - (void) loadXMLString;  
  12. - (void) loadXMLData;  
  13. - (void) loadArticles;  
  14. - (void) loadUnknownXML;  
  15. - (void) traverseElement:(TBXMLElement *)element;  
  16.   
  17. @end  

 

.m

Java代码  收藏代码
  1. - (void)viewDidLoad  
  2. {  
  3.     [self loadURL];  
  4.     [self loadArticles]; //  打印xml内容,以及存储内容到数组中  
  5.     [self loadXMLString];  
  6.     [self loadXMLData];  
  7.     [self loadUnknownXML];  
  8.       
  9.     [super viewDidLoad];  
  10.     // Do any additional setup after loading the view, typically from a nib.  
  11. }  
  12.   
  13. - (void)loadURL {  
  14.     // Load and parse an xml string  
  15.     tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:@"http://feeds.feedburner.com/IbtimescomWorld?format=xml"]];  
  16.       
  17.     // If TBXML found a root node, process element and iterate all children  
  18.     if (tbxml.rootXMLElement)  
  19.         [self traverseElement:tbxml.rootXMLElement];  
  20.     
  21.     // release resources  
  22.     tbxml = nil;  
  23. }  
  24.   
  25. - (void)loadArticles {  
  26.     // Load and parse an xml string  
  27.     tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:@"http://feeds.feedburner.com/IbtimescomWorld?format=xml"]];  
  28.       
  29.     //tbxml = [TBXML tbxmlWithXMLFile:@"books.xml"];  
  30.   
  31.     TBXMLElement *root = tbxml.rootXMLElement;  
  32.   
  33.     if (root)   
  34.     {  
  35.         TBXMLElement *channel = [TBXML childElementNamed:@"channel" parentElement:root];  
  36.         if (channel)   
  37.         {  
  38.             articles = [NSMutableArray array];  
  39.               
  40.             TBXMLElement *item = [TBXML childElementNamed:@"item" parentElement:channel];  
  41.             while (item)   
  42.             {  
  43.                 TBXMLElement *title = [TBXML childElementNamed:@"title" parentElement:item];  
  44.                 TBXMLElement *link = [TBXML childElementNamed:@"link" parentElement:item];  
  45.                 TBXMLElement *description = [TBXML childElementNamed:@"description" parentElement:item];  
  46.                 TBXMLElement *date = [TBXML childElementNamed:@"pubDate" parentElement:item];  
  47.                   
  48.                 //NSLog(@"\n title => %@",[TBXML textForElement:title]);  
  49.                   
  50.                 NSDictionary *newsItem = [NSDictionary dictionaryWithObjectsAndKeys:  
  51.                                           [TBXML textForElement:title], @"title",  
  52.                                           [TBXML textForElement:link], @"link",  
  53.                                           [TBXML textForElement:description], @"desc",  
  54.                                           [TBXML textForElement:date], @"date",  
  55.                                           nil];  
  56.                   
  57.                 [articles addObject:newsItem];  
  58.                 item = [TBXML nextSiblingNamed:@"item" searchFromElement:item];  
  59.             }  
  60.         }  
  61.     }   
  62.       
  63.     for(int i=0;i<[articles count];i++)    
  64.     {    
  65.         NSLog(@"%@",[articles objectAtIndex:i]);    
  66.     }    
  67.   
  68.     tbxml = nil;  
  69. }  
  70.   
  71. - (void)loadXMLString {  
  72.     // Load and parse an xml string  
  73.     tbxml = [[TBXML alloc] initWithXMLString:@"<root><elem1 attribute1=\"elem1-attribute1\"/><elem2 attribute2=\"attribute2\"/></root>"];  
  74.       
  75.     // If TBXML found a root node, process element and iterate all children  
  76.     if (tbxml.rootXMLElement)  
  77.         [self traverseElement:tbxml.rootXMLElement];  
  78.       
  79.     // release resources  
  80.     tbxml = nil;  
  81. }  
  82.   
  83. - (void)loadXMLData {  
  84.     // Load and parse an NSData object  
  85.     NSString * xmlString = @"<root><elem1 attribute1=\"elem1-attribute1\"/><elem2 attribute2=\"attribute2\"/></root>";  
  86.     NSData * xmlData = [xmlString dataUsingEncoding:NSASCIIStringEncoding];  
  87.       
  88.     tbxml = [[TBXML alloc] initWithXMLData:xmlData];  
  89.       
  90.     // If TBXML found a root node, process element and iterate all children  
  91.     if (tbxml.rootXMLElement)  
  92.         [self traverseElement:tbxml.rootXMLElement];  
  93.       
  94.     // release resources  
  95.     tbxml = nil;  
  96. }  
  97.   
  98. - (void)loadUnknownXML {      
  99.     // Load and parse the books.xml file  
  100.     tbxml = [[TBXML alloc] initWithXMLFile:@"books" fileExtension:@"xml"];  
  101.       
  102.     // If TBXML found a root node, process element and iterate all children  
  103.     if (tbxml.rootXMLElement)  
  104.         [self traverseElement:tbxml.rootXMLElement];  
  105.       
  106.     // release resources  
  107.     tbxml = nil;  
  108. }  
  109.   
  110. - (void) traverseElement:(TBXMLElement *)element {  
  111.       
  112.     do {  
  113.         // Display the name of the element  
  114.         NSLog(@"%@",[TBXML elementName:element]);  
  115.           
  116.         // Obtain first attribute from element  
  117.         TBXMLAttribute * attribute = element->firstAttribute;  
  118.           
  119.         // if attribute is valid  
  120.         while (attribute) {  
  121.             // Display name and value of attribute to the log window  
  122.             NSLog(@"%@->%@ = %@",[TBXML elementName:element],[TBXML attributeName:attribute], [TBXML attributeValue:attribute]);  
  123.               
  124.             // Obtain the next attribute  
  125.             attribute = attribute->next;  
  126.         }  
  127.           
  128.         // if the element has child elements, process them  
  129.         if (element->firstChild) [self traverseElement:element->firstChild];  
  130.           
  131.         // Obtain next sibling element  
  132.     } while ((element = element->nextSibling));    
  133. }  

如果你关闭了ARC,请替换 tbxml = nil; 为 [tbxml release];

如果你开启了ARC,会有类似“arc forbids explicit message send of ...”这样的错误,那么你需要转换一下:

 

 

Edit => Refactor => Convert to Objective-C ARC...

 

其实就是把 NSDataAdditions.m 中的 [result autorelease] 替换成 return result;

 

或者干脆关闭ARC,方法为:打开当前工程,打开"Build Settings",找到Objective-C Automatic Reference Counting项,将它的值设置为NO。

 

 

本实例为ARC开启

 

全部API为:

 

http://www.tbxml.co.uk/TBXML/API.html

 

 

方法

 

 

1.实例化

 

+ (id)tbxmlWithXMLFile:(NSString*)aXMLFile;

用xml文件名(包括扩展名)实例化一个tbxml对象

例如:TBXML * tbxml = [[TBXML alloc] initWithXMLFile:@”books.xml”];

 

- (id)initWithXMLFile:(NSString*)aXMLFile fileExtension:(NSString*)aFileExtension

用xml文件名和扩展名实例化一个tbxml对象

例如:TBXML * tbxml = [[TBXML alloc] initWithXMLFile:@”books” fileExtension:@”xml”];

 

- (id)initWithXMLString:(NSString*)aXMLString

用一段xml内容代码来实例化一个tbxml对象

例如:tbxml = [[TBXML alloc] initWithXMLString:@”<root><elem1 attribute1=”elem1 attribute1″/><elem2 attribute2=”elem2 attribute2″/></root>;”];

 

- (id)initWithXMLData:(NSData*)aData

用一个封装了xml内容的NSData对象来实例化tbxml对象

例如:TBXML * tbxml = [[TBXML alloc] initWithXMLData:myXMLData];

 

- (id)initWithURL:(NSURL*)aURL

用一个URL来实例化一个tbxml

例如:tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:@"http://www.ifanr.com/feed"]];

 

 

2.成员方法

 

+ (TBXMLElement*) childElementNamed:(NSString*)aName parentElement:(TBXMLElement*)aParentXMLElement

获得aParentXMLElement元素的首个名字为aName的元素

例如:TBXMLElement * author = [TBXML childElementNamed:@"author" parentElement:rootXMLElement];

 

+ (TBXMLElement*) nextSiblingNamed:(NSString*)aName searchFromElement:(TBXMLElement*)aXMLElement

返回下一个名为aName的兄弟元素

例如:TBXMLElement * author = [TBXML nextSiblingNamed:@"author" searchFromElement:author];

 

+ (NSString*) valueOfAttributeNamed:(NSString *)aName forElement:(TBXMLElement*)aXMLElement

返回aXMLElement元素中,名为aName的属性的属性值。

例如:NSString * authorName = [TBXML valueOfAttributeNamed:@"name" forElement:authorElement];

 

+ (NSString*) textForElement:(TBXMLElement*)aXMLElement

返回元素aXMLElement的text值

例如:NSString * bookDescription = [TBXML textForElement:bookElement];

 

+ (NSString*) elementName:(TBXMLElement*)aXMLElement;

返回元素aXMLElement的标签名

例如:NSString * elementName = [TBXML elementName:element];

 

+ (NSString*) attributeName:(TBXMLAttribute*)aXMLAttribute;

返回属性aXMLAttribute的属性名

例如:NSString * attributeName = [TBXML attributeName:attribute];

 

+ (NSString*) attributeValue:(TBXMLAttribute*)aXMLAttribute;

返回属性aXMLAttribute的属性值

例如:NSString * attributeValue = [TBXML attributeValue:attribute];

 

常用的基本就这些,通过合理的迭代,递归等组合调用,基本可以解决所有的解析问题。

 

 

 

 

抱歉!评论已关闭.