OnoXML/HTML 解析庫
Ono (斧)是 iOS & Mac OSX 處理 XML & HTML 的一種明智的方法(解析庫)。
XML 在 Cocoa 的支持并不好,NSXMLParser 強(qiáng)制要求繁瑣的委托模式,這是非常不方便實(shí)現(xiàn)的。 NSXMLDocument 有一點(diǎn)好用,但只能在 Mac OS X 工作, 但占用大量內(nèi)存。
不管你的 app 是否需要 XML-RPC web服務(wù)的接口,爬一個(gè)網(wǎng)頁,或者解析一個(gè) RSS feed,Ono 會(huì)讓你的工作更輕松。
特性
簡單,現(xiàn)代的 API 遵循標(biāo)準(zhǔn) Objective-C 公約,包括擴(kuò)展使用塊和
NSFastEnumeration極高性能文檔解析和遍歷, powered by
libxml2自動(dòng)轉(zhuǎn)換的日期和數(shù)字值
正確,正常地處理元素和屬性的 XML 命名空間
能夠從
NSString或NSData加載 HTML 和 XML 文檔完整文檔
全面的測試套件
使用:
#import "Ono.h"
NSData *data = ...;
NSError *error;
ONOXMLDocument *document = [ONOXMLDocument XMLDocumentWithData:data error:&error];
for (ONOXMLElement *element in document.rootElement.children) {
NSLog(@"%@: %@", element.tag, element.attributes);
}
// Support for Namespaces
NSString *author = [[document.rootElement firstChildWithTag:@"creator" inNamespace:@"dc"] stringValue];
// Automatic Conversion for Number & Date Values
NSDate *date = [[document.rootElement firstChildWithTag:@"created_at"] dateValue]; // ISO 8601 Timestamp
NSInteger numberOfWords = [[[document.rootElement firstChildWithTag:@"word_count"] numberValue] integerValue];
BOOL isPublished = [[[document.rootElement firstChildWithTag:@"is_published"] numberValue] boolValue];
// Convenient Accessors for Attributes
NSString *unit = [document.rootElement firstChildWithTag:@"Length"][@"unit"];
NSDictionary *authorAttributes = [[document.rootElement firstChildWithTag:@"author"] attributes];
// Support for XPath & CSS Queries
[document enumerateElementsWithXPath:@"http://Content" usingBlock:^(ONOXMLElement *element, NSUInteger idx, BOOL *stop) {
NSLog(@"%@", element);
}];
評論
圖片
表情
