您的位置:首页 > 其它

[iphone]XML 解析 之 TBXML 介绍

2011-09-19 12:27 387 查看
总的来说,iphone 上可用的解析XML的方式大概有2种类型的。

1.Tree-based API:这种API的处理方式是将XML的结构看成是树,然后把树的各部分看成一个对象来处理,这就是我们说的DOM
(Document Object Model)方式。在iPhone的SDK里包含了一个libxml2的框架(Framework)就能进行DOM解析方式。Google的GDataXML也是基于libxml2的,因此在使用GDataXML之前,你需要先导入libxml2。

2.Event-driven API:这种方式通常用于解析基于的事件,SAX解析方式就是这种解析方式的代表。在iPhone开发的,也可以利用这种方式来解析XML,不过这可不是Iphone
SDK的属性啊,而是Objective-C的功能。在Objectvie-C种有专门解析XML的类NSXMLParser。

r TBXML are:

XML files conforming to the
W3C
XML spec 1.0 should be passable

XML parsing should incur the fewest possible resources

XML parsing should be achieved in the shortest possible time

It shall be easy to write programs that utilise TBXML

Design Goals

Check out this post for a good comparison of XML parsers. “How To Chose The Best XML Parser for Your iPhone Project

Performance

TBXML.zip

TBXML-Books.zip

Version changes

介绍一种轻量级的XML解析方式,TBXML。

按着以上两个分类的话,算是DOM 的解析方式。需要去找寻root 节点。然后按着名字顺序查找。找到某个节点之后,如果需要找寻孩子信息,那么使用这个节点做为parent,继续向里面查找。

正如他的官方介绍所说TBXML 具有如下特点:

速度快,效率高,占用的额外资源少。耗时短。

本人看来,用起来确实方便。上手容易,快速。

常用的初始化方式有。如下几种。

+ (id)tbxmlWithURL:(NSURL*)aURL;
+ (id)tbxmlWithXMLString:(NSString*)aXMLString;
+ (id)tbxmlWithXMLData:(NSData*)aData;
+ (id)tbxmlWithXMLFile:(NSString*)aXMLFile;
+ (id)tbxmlWithXMLFile:(NSString*)aXMLFile
fileExtension:(NSString*)aFileExtension;

- (id)initWithURL:(NSURL*)aURL;
- (id)initWithXMLString:(NSString*)aXMLString;
- (id)initWithXMLData:(NSData*)aData;
- (id)initWithXMLFile:(NSString*)aXMLFile;
- (id)initWithXMLFile:(NSString*)aXMLFile
fileExtension:(NSString*)aFileExtension;

@end

静态方法也不过7种而已。

// ================================================================================================
//
TBXML Static Functions Interface
// ================================================================================================

@interface TBXML (StaticFunctions)

+ (NSString*) elementName:(TBXMLElement*)aXMLElement;
+ (NSString*) textForElement:(TBXMLElement*)aXMLElement;
+ (NSString*) valueOfAttributeNamed:(NSString
*)aName forElement:(TBXMLElement*)aXMLElement;

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

+ (TBXMLElement*) nextSiblingNamed:(NSString*)aName
searchFromElement:(TBXMLElement*)aXMLElement;
+ (TBXMLElement*) childElementNamed:(NSString*)aName
parentElement:(TBXMLElement*)aParentXMLElement;

@end

上一个本人实际的例子,大家一看就行,你懂的。。

+(NSString *)getOPFFolderName:(NSString
*)prefixName
{
//doc/ldj/META-INF/container.xml;
//<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
//
<rootfiles>
//
<rootfile full-path="OPS/fb.opf" media-type="application/oebps-package+xml"/>
//
</rootfiles>
//
</container>
NSString *path
= [NSString
stringWithFormat:[NSHomeDirectory()
stringByAppendingPathComponent:
@"/Documents/%@/META-INF/container.xml"],prefixName];

TBXML *containeXML = [TBXML
tbxmlWithURL:[NSURL
fileURLWithPath:path]];
TBXMLElement *root = containeXML.rootXMLElement;
//TBXMLElement *container= [TBXML childElementNamed:@"container" parentElement:root];

TBXMLElement *rootfile
= [TBXML
childElementNamed:@"rootfile"
parentElement:
[TBXML
childElementNamed:@"rootfiles"
parentElement:
root]];

return [TBXML
valueOfAttributeNamed:@"full-path"
forElement:rootfile];
}

参考文章:
http://www.tbxml.co.uk/TBXML/TBXML_Free.html http://www.norkoo.com/show/New_Tech/IPhone/dfdikhjijkagekigicgkgcdkiheb.aspx 转自:http://www.cnblogs.com/AlexLiu/archive/2010/10/25/1860292.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: