就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巨合适不过了!
具体的api参见tbxml_api:http://pimacun.72pines.com/2010/12/31/tbxml_api/
分享到:
相关推荐
本文将深入探讨如何在iOS中使用TBXML库进行XML解析,并通过实例来展示如何打印XML内容以及将其存储到数组中。 TBXML是专为iOS设计的一个轻量级、高效的XML解析库,由TonyBennett开发。它被广泛应用于需要快速解析...
TBXML采用SAX解析方式,但提供了更简洁的API,使得开发者可以更容易地访问XML数据。 TBXML的使用步骤: 1. 引入库:首先,将TBXML库(如TBXML-master)导入到Xcode项目中。你可以通过CocoaPods、Carthage或手动...
本文将深入探讨两种在iOS中解析XML的主要方法:苹果自带的NSXMLParser以及第三方库TBXML。 首先,苹果提供的NSXMLParser是Objective-C中的一个类,它遵循事件驱动的模型来解析XML文档。当XML文档被解析时,...
iOS xml 解析 本来是喜欢用json的,可服务端的那位大神居然搞不出来 他说他是直接返回一串字符串的,是底层给疯装成xml返回的 嗨,累 网上搜了下,有说用sdk自带的NSXMLParse, 有说用google提供的GDataXML, 还有...
TBXML, 超快速,轻量,易于使用的Mac & iOS的XML解析器 什么是 TBXMLTBXML是一个轻量级的XML文档解析器,用 objective-c 设计,用于苹果 iPad,iPhone & iPod ( 。Mac OSX兼容) 触发器。 TBXML旨在提供最快的XML解析...
tbxml-android: 版本 1.00.0TBXML - Android NDK 端口tbxml-android是 TBXML XML 解析库(适用于 iOS)到 Android NDK 的端口。 它适用于那些偶尔需要 SAX 解析器的速度和 DOM 的便利性并且可以使用非常简单的 XML ...
ios xml解析 NSXMLParser。 NSXMLParser是基于SAX的解析方式。NSXMLParser采用了委托设计模式,因此他的实现类需要采用协议并支持委托。NSXMLParser解析XML需要委托NSXMLParserDelegate实现。
TouchXML是另一个Objective-C的XML解析库,它基于Cocoa Touch框架,设计上更加面向对象。TouchXML提供了一个类似NSXMLParser的接口,但解析速度更快,内存管理更好。然而,由于苹果在后来的iOS版本中提供了内置的...
在iPhone开发中,XML的解析有很多选择,iOS SDK提供了NSXMLParser和libxml2两个类库,另外还有很多第三方类库可选,例如TBXML、TouchXML、KissXML、TinyXML和GDataXML。问题是应该选择哪一个呢? 解析 XML 通常有两...
在这种情况下,我们使用NSXMLParser来解析XML数据。NSXMLParser是Apple提供的原生XML解析器,它可以逐行解析XML文档,通过代理方法提供事件驱动的解析体验。在解析过程中,我们可以监听如“元素开始”、“元素结束”...
TBXML是一个轻量级、高效的XML解析库,专为iOS和Mac OS X设计。它通过最小化内存占用和提高解析速度,帮助开发者快速处理XML数据。TBXML库通过二进制编码XML文档,从而减少了内存开销,尤其适合处理大型XML文件。...
在某些情况下,如果你的图片数据存储在XML文件中,可能需要使用TBXML来提取图片URL,然后进行异步加载。下面是如何使用TBXML结合GCD进行异步加载图片的步骤: 1. **解析XML**:使用TBXML解析XML文件,获取图片的URL...
1. **概述**:TBXML 是一个轻量级的DOM模式解析库,主要用于解析XML文档。它不支持XML文档验证和XPath查询功能,但可以用来读取XML数据。 2. **特点**: - 轻量级:相比于其他解析器,TBXML占用资源较少。 - 不...
- **TBXML**:一个高效的XML解析器,适用于iOS平台,可以用于处理游戏中的数据。 #### 五、主要内容概述 - **游戏开发基础**:介绍游戏开发的基本概念,包括游戏设计原则、开发流程和技术选型等。 - **iOS平台简介...
3. **编程语言与框架**:重点讲解Objective-C和Swift这两种常用的iOS开发语言,以及如何使用它们结合Cocoa Touch框架进行游戏逻辑的编写。 4. **图形与动画处理**:介绍如何利用OpenGL ES和SpriteKit等技术实现高...
- **XML解析:** 虽然不内置支持,但可以使用第三方库如`TBXML`或`KissXML`配合`AFNetworking`处理XML数据。 **4. 文件上传与下载** - **文件上传:** 可以通过`setUploadProgressBlock:`设置进度回调,`...