Swift實現的輕量快速的 XML/HTML 解析器:Fuzi

jopen 9年前發布 | 66K 次閱讀 Fuzi XML操作類庫

Swift實現的輕量快速的 XML/HTML 解析器。

Mattt Thompson大神的 Ono(斧) 是iOS/OSX平臺上非常好用的一個XML/HTML 解析庫。用ObjectiveC實現的Ono在Swift的應用里雖然可以使用,卻有諸多不便。因此鄙人參照了Ono對libxml2的封裝方式,對類和方法進行了重新設計弄出了這個小庫。同時修正了Ono存在的一些邏輯上和內存管理方面的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)
}

項目主頁:http://www.baiduhome.net/lib/view/home/1442670891852

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!