Zewo XML - Swift的XML/HTML解析器

Zewo XML - Swift的XML/HTML解析器

Swift HTML操作

访问GitHub主页

共10Star

详细介绍

XML

Swift Zewo Platform License Slack Travis Codebeat

XML is a Swift libxml2 wrapper for parsing XML/HTML.

Features

  • Build XML/HTML Tree and Navigate
  • XPath Query Supported
  • Comprehensive Unit Test Coverage
  • CSS Selector (on going)

Usage

  • Init with String:
let xmlString = "<?xml version='1.0' encoding='UTF-8'?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>"
let xmlDoc = XMLDocument(xmlString: xmlString)
let bodyNode = xmlDoc?.rootNode?.firstChildWithName("body")
print("body: \(bodyNode?.content)") // body: Optional("Don\'t forget me this weekend!")
  • Init with Data:
let file = File(path: "~/Desktop/google.com.html")
let googleIndexData = try? file.read(file.length)
if let googleIndexData = googleIndexData {
    let xmlDoc = XMLDocument(htmlData: googleIndexData)!
    let htmlNode = xmlDoc.rootNode!
    print("html tagName: \(htmlNode.tagName)") // html tagName: Optional("html")

    let aNodes = xmlDoc.xPath("//body//a")
    if let firstANode = aNodes?.first {
        print("first a node tagName: \(firstANode.name)") // first a node tagName: Optional("a")
        let href = firstANode["href"]
        print("first a node href: \(href)") // first a node href: Optional("http://www.google.ca/imghp?hl=en&tab=wi")
    }
} else {
    print("can't read google.com.html")
}

Installation

  • OSX
brew install libxml2
brew link --force libxml2
  • Linux
sudo apt-get install libxml2-dev 
  • Package.swift
import PackageDescription

let package = Package(
    dependencies: [
        .Package(url: "https://github.com/Zewo/XML.git", majorVersion: 0, minor: 10),
    ]
)
  • OSX
swift build
  • Linux
swift build -Xcc -I/usr/include/libxml2 -Xlinker -rpath -Xlinker $(pwd)/.build/debug/

Support

If you need any help you can join our Slack and go to the #help channel. Or you can create a Github issue in our main repository. When stating your issue be sure to add enough details, specify what module is causing the problem and reproduction steps.

Community

Slack

The entire Zewo code base is licensed under MIT. By contributing to Zewo you are contributing to an open and engaged community of brilliant Swift programmers. Join us on Slack to get to know us!

License

This project is released under the MIT license. See LICENSE for details.

推荐源码