FuziXML/HTML 解析器
Swift 實現(xiàn)的輕量快速的 XML/HTML 解析器。
Mattt Thompson大神的 Ono (斧) 是iOS/OSX平臺上非常好用的一個XML/HTML 解析庫。用ObjectiveC實現(xiàn)的Ono在Swift的應用里雖然可以使用,卻有諸多不便。因此鄙人參照了Ono對libxml2的封裝方式,對類和方法進行了重新設(shè)計弄出了這個小庫。同時修正了Ono存在的一些邏輯上和內(nèi)存管理方面的bug。
中文README
https://github.com/cezheng/Fuzi/blob/master/README-zh.md
示例代碼:
let xml = "..."
do {
let document = try XMLDocument(string: xml)
if let root = document.root {
// Accessing all child nodes of root element
for element in root.children {
print("\(element.tag): \(element.attributes)")
}
// Getting child element by tag & accessing attributes
if let length = root.firstChild(tag:"Length", inNamespace: "dc") {
print(length["unit"]) // `unit` attribute
print(length.attributes) // all attributes
}
}
// XPath & CSS queries
for element in document.xpath("") {
print("\(element.tag): \(element.attributes)")
}
if let firstLink = document.firstChild(css: "a, link") {
print(firstLink["href"])
}
} catch let error {
print(error)
}評論
圖片
表情
