介绍一种轻量级的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