就xml解析来讲,目前用过的最简洁,速度最快的当属tbxml,是基于C框架的所以直接拿在iPhone上用了。

先说下用法,把tbxml的4个文件拖入class,然后为工程添加libz.dylib框架即可。

废话就不说了,直接看代码,如下:

定义了两个方法(其中一个带着递归子方法),分别处理已知结构和未知结构的xml。

//调用

- (void)viewDidLoad {
tbXml = [TBXML tbxmlWithXMLFile:@"books.xml"];

TBXML *tbXml2 = [TBXML tbxmlWithXMLString:@"<root><elem1 attribute1=\"elem1-attribute1\"/><elem2 attribute2=\"attribute2\"/></root>"];
TBXML *tbXml3 = [[TBXML alloc] initWithURL:[NSURL URLWithString:@"http://www.ifanr.com/feed"]];

[self testTBXM:tbXml];
[self dealUnknow:tbXml3];

[super viewDidLoad];
}

1.解析已知结构的xml。先看下xml的基本结构:

<?xml version="1.0"?>
<authors>
<author name="J.K. Rowling">
<book title="Harry Potter and the Philosopher's Stone" price="119.99">
<description>
Harry potter thinks he is an ordinary boy - until he is rescued from a beetle-eyed giant of a man, enrolls at Hogwarts School of Witchcraft and Wizardry, learns to play quidditch and does battle in a deadly duel.
</description>
</book>
<book title="Harry Potter and the Chamber of Secrets" price="8.99">
<description>
When the Chamber of Secrets is opened again at the Hogwarts School for Witchcraft and Wizardry, second-year student Harry Potter finds himself in danger from a dark power that has once more been released on the school.
</description>
</book>
<book title="Harry Potter and the Prisoner of Azkaban" price="12.99">
<description>
Harry Potter, along with his friends, Ron and Hermione, is about to start his third year at Hogwarts School of Witchcraft and Wizardry. Harry can't wait to get back to school after the summer holidays. (Who wouldn't if they lived with the horrible Dursleys?) But when Harry gets to Hogwarts, the atmosphere is tense. There's an escaped mass murderer on the the loose, and the sinister prison guards of Azkaban have been called in to guard the school.
</description>
</book>
</author>
<author name="Douglas Adams">
<book title="The Hitchhiker's Guide to the Galaxy" price="15.49">
<description>
Join Douglas Adams's hapless hero Arthur Dent as he travels the galaxy with his intrepid pal Ford Prefect, getting into horrible messes and generally wreaking hilarious havoc.
</description>
</book>
<book title="The Restaurant at the End of the Universe " price="14.36">
<description>
Arthur and Ford, having survived the destruction of Earth by surreptitiously hitching a ride on a Vogon constructor ship, have been kicked off that ship by its commander. Now they find themselves aboard a stolen Improbability Drive ship commanded by Beeblebrox, ex-president of the Imperial Galactic Government and full-time thief.
</description>
</book>
</author>
</authors>

//解析代码

- (void)testTBXM:(TBXML *)tbx {  
TBXMLElement *root = tbx.rootXMLElement;
TBXMLElement *author = [TBXML childElementNamed:@"author" parentElement:root];
NSString *name = [TBXML valueOfAttributeNamed:@"name" forElement:author];
NSLog(@"author:%@", name);
TBXMLElement *book = [TBXML childElementNamed:@"book" parentElement:author];
TBXMLElement *descriptionElem = [TBXML childElementNamed:@"description" parentElement:book];
NSString * description = [TBXML textForElement:descriptionElem];
NSLog(@"author:%@", description); 
}

2.递归解析未知结构的xml。

//主调用方法

- (void)dealUnknow:(TBXML *)tbx {
if (tbx.rootXMLElement) {
TBXMLElement *element = tbx.rootXMLElement;  
[self recurrence:element];
}
else {
NSLog(@"Format Error!");

}

//递归子方法

- (void)recurrence:(TBXMLElement *)element {
do {

NSLog(@"<%@>:{%@}",[TBXML elementName:element], [TBXML textForElement:element]);// Display the name of the element

//迭代处理所有属性
TBXMLAttribute * attribute = element->firstAttribute;
while (attribute) {
//显示
NSLog(@"<%@>->[%@ = %@]", [TBXML elementName:element], [TBXML attributeName:attribute], [TBXML attributeValue:attribute]);
//迭代
attribute = attribute->next;
}

//递归处理子树
if (element->firstChild) {
[self recurrence:element->firstChild];
}

//迭代处理兄弟树
} while ((element = element->nextSibling)); 
}

总之,tbxml解析xml飞速、简洁,可惜不能操作回写,仅陷于解析,做rss巨合适不过了!

如想进步了解,请参加下篇,tbxml常用api:

==============================================================================

tbxml常用api:
文件

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

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

超级简洁的xml解析框架:TBXML相关推荐

  1. 制衣厂普工小伙用java代码写的xml解析框架

    xml解析框架,这个框架也可以解析html,是我自己写的xml解析技术,花费我很多的时间,我在工厂做这个工作来维持生计↓↓↓↓↓↓↓↓ 哈哈哈,开个玩笑,下面这张图片才是我,这张图片是主管拍的 平均月 ...

  2. [iphone]XML 解析 之 TBXML 介绍

    总的来说,iphone 上可用的解析XML的方式大概有2种类型的. 1.Tree-based API:这种API的处理方式是将XML的结构看成是树,然后把树的各部分看成一个对象来处理,这就是我们说的D ...

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

    关联:iOS中XML解析 (二) libxml2(实例:打印xml内容及存储到数组) 在时间上TBXML占优,libxml2支持了边下载边解析. 来源:http://www.codeios.com/f ...

  4. java xml开源操作类,xml解析和操作的开源工具项目涵盖java c++ php 等语言

    XML解析器-Xerces    XML操作库-dom4j    XML文档解析器-Digester    J2ME-的XML-解析器-kXML XML解析类库-MXP1    XML解析器-LibX ...

  5. iOS中XML解析 (二) libxml2(实例:打印xml内容及存储到数组)

    关联:iOS中XML解析 (一) TBXML (实例:打印xml内容及存储到数组) 关于libxml库的基本使用,在http://xmlsoft.org/网上有文档. 准备工作: project=&g ...

  6. 常用 XML 解析技术

    现在的软件项目都不是独立的一个项目,都是多系统协调工作.这样的话就涉及到系统间的通讯,通讯就会跟报文传输挂上关系.系统间使用怎样的报文格式进行通讯呢?有的使用固定长度格式报文:有的使用变长格式报文:有 ...

  7. java 数学公式解析框架有哪些_开源工具 | 推荐几个Gitee火热Java项目

    openEA开源周刊 openEA开源社区的官方运营载体 这里每天给大家呈现有价值的开源资讯,欢迎您的来稿与推荐,点击上方蓝色字,加入我们吧! 摘要:OkHttps 是近期开源的对 OkHttp3 轻 ...

  8. HttpSender OkHttp+RxJava超好用、功能超级强大的Http请求框架

    HttpSender HttpSender 是对OkHttp二次封装,并与RxJava做到了无缝连接,支持任意Http请求方式,如:Get.Post.Head.Put等:也支持任意数据解析方法,如:J ...

  9. 网络数据的XML解析

    XML解析一般分两种模式SAX和DOM,事件和文档.具体解析google去吧,有详细.不过看了下面的两个例子,一般就了解了. 一:XML解析之SAX解析,以及对NSXMLParser的应用. sax解 ...

最新文章

  1. Android的Adapter用法总结
  2. C#窗体控件更新(五)
  3. SAP产品增强技术回顾
  4. 地图市场三足鼎立:诺基亚官方确认与亚马逊合作
  5. 看到别人的简历,mark一下。
  6. 小米12系列核心配置曝光:骁龙898+双百瓦快充+2亿像素主摄
  7. java二维码生成-谷歌(Google.zxing)开源二维码生成学习及实例
  8. Linux下设备驱动
  9. 高速PCB设计中走线屏蔽的各项规则解析
  10. OL3+中链家地图找房功能实现
  11. 阿里巴巴重要开源项目汇总(转载)
  12. poi生成pptx文件以及背景颜色或者背景图片的设置
  13. DFU u-boot搭建
  14. 芯片设计五部曲之一 | 声光魔法师——模拟IC
  15. QKX-ZSZ-4000岩体真三轴动静载荷-应力应变曲线绘制软件
  16. 工业机器人编程语言入门_人工智能和机器学习入门的5种编程语言
  17. MU-MIMO技术让网络变神速的方法
  18. 后端面试知识点总结 数据库 mysql
  19. 安卓图片分类浏览器php,Android快速实现图片浏览
  20. Doxygen详细介绍

热门文章

  1. Java多线程之可见性之volatile
  2. RabbitMQ学习总结(一)——基础概念详细介绍
  3. 亚马逊的新Linux发行版对红帽造成了威胁
  4. 2017-03-10Git撤销修改
  5. [cb]ScriptableObject 序列化
  6. Android开源项目汇总
  7. How To Fix: SCP And SSH Login Prompt Is Very Sl...
  8. 在Centos下启用mysql的远程访问账号
  9. 创新也要懂“规矩” 协同软件将走向何方
  10. php tp3.2 去重方法,thinkPHP框架整合tcpdf插件操作示例