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

TBXML常用API

2018年05月25日 ⁄ 综合 ⁄ 共 3350字 ⁄ 字号 评论关闭

==========================================================
文件

tbxml“框架”中包含的文件:
TBXML.h - tbxml声明
TBXML.m - tbxml实现
NSDataAdditions.h - NSData类别等的声明
NSDataAdditions.m - NSData类别等的实现,包括base64,gzip,NSData类别等等

==========================================================
结构体

TBXMLElement结构体,包含XML中对应element的信息. 包括元素标签名、元素text值、指向第一个属性对象的指针、父元素、首个子元素,以及下一个兄弟元素.可以用这个结构体创建一个链表(树)来表示一个完整的xml文件.
结构如下:
typedef struct _TBXMLElement {
char * name;
char * text;
TBXMLAttribute * firstAttribute;
struct _TBXMLElement * parentElement;
struct _TBXMLElement * firstChild;
struct _TBXMLElement * currentChild;
struct _TBXMLElement * nextSibling;
struct _TBXMLElement * previousSibling;
} TBXMLElement;

TBXMLAttribute结构体,包含了xml中的属性信息. 包括属性名、属性值和下一个兄弟属性对象的指针. 使用这个结构可以创建一个Element的属性链表.
typedef struct _TBXMLAttribute {
char * name;
char * value;
struct _TBXMLAttribute * next;
} TBXMLAttribute;

TBXMLElementBuffer结构体,是用来缓存TBXMLElement结构体对象的. 当被使用时, 将新建一个缓存区并连接到前一个上(链表).这样可以有效的管理Element在内存的创建和回收.
typedef struct _TBXMLElementBuffer {
TBXMLElement * elements;
struct _TBXMLElementBuffer * next;
struct _TBXMLElementBuffer * previous;
} TBXMLElementBuffer;

TBXMLAttributeBuffer结构体,是用来缓存TBXMLAttribute对象的. 当被使用时, 将新建一个缓存区并连接到前一个上(链表). 这样可以有效的管理Attribute在内存的创建和回收.
typedef struct _TBXMLAttributeBuffer {
TBXMLAttribute * attributes;
struct _TBXMLAttributeBuffer * next;
struct _TBXMLAttributeBuffer * previous;
} TBXMLAttributeBuffer;

==========================================================
方法

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];

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

详细示例参见:http://pimacun.72pines.com/2010/12/31/tbxml/

抱歉!评论已关闭.