Ji: a Swift XML/HTML parser

Ji 戟

CI Status CocoaPods Version Carthage Compatible License Platform

Ji (戟) 是一个基于 libxml2 的 Swift 封装库,用于解析 XML/HTML。

特性

要求

安装

CocoaPods

要使用 CocoaPods 将 Ji 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它

use_frameworks!

pod 'Ji', '~> 5.0.0'

然后,运行以下命令

$ pod install

Carthage

要使用 Carthage 将 Ji 集成到您的 Xcode 项目中,请在您的 Cartfile 中指定它

github "honghaoz/Ji" ~> 5.0.0

Swift Package Manager (SPM)

先决条件

brew install libxml2
brew link --force libxml2
$ sudo apt-get install libxml2-dev

更新 Package.swift

要在您的项目中集成 Ji,请将正确的描述添加到您的 Package.swift 文件

// swift-tools-version:5.0
import PackageDescription

let package = Package(
    name: "YOUR_PROJECT_NAME",
    dependencies: [
        .package(url: "https://github.com/honghaoz/Ji.git", from: "5.0.0")
    ],
    targets: [
        .target(
            name: "YOUR_TARGET_NAME",
            dependencies: ["Ji"]
        ),
        ...
    ]
)

手动

如果您不想使用依赖管理器,您可以手动将 Ji 集成到您的项目中。

用法

如果您使用 CocoaPods 来集成 Ji。 首先导入 Ji

import Ji
let jiDoc = Ji(htmlURL: URL(string: "http://www.apple.com/support")!)
let titleNode = jiDoc?.xPath("//head/title")?.first
print("title: \(String(describing: titleNode?.content))") // title: Optional("Official Apple Support")
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 jiDoc = Ji(xmlString: xmlString)
let bodyNode = jiDoc?.rootNode?.firstChildWithName("body")
print("body: \(String(describing: bodyNode?.content))") // body: Optional("Don\'t forget me this weekend!")
let googleIndexData = try? Data(contentsOf: URL(string: "http://www.google.com")!)
if let googleIndexData = googleIndexData {
    let jiDoc = Ji(htmlData: googleIndexData)!
    let htmlNode = jiDoc.rootNode!
    print("html tagName: \(String(describing: htmlNode.tagName))") // html tagName: Optional("html")

    let aNodes = jiDoc.xPath("//body//a")
    if let firstANode = aNodes?.first {
        print("first a node tagName: \(String(describing: firstANode.name))") // first a node tagName: Optional("a")
        let href = firstANode["href"]
        print("first a node href: \(String(describing: href))") // first a node href: Optional("http://www.google.ca/imghp?hl=en&tab=wi")
    }
} else {
    print("google.com is inaccessible")
}

let 戟文档 = (htmlURL: URL(string: "https://cocoapods.org.cn/pods/Ji")!)
let attribution = 戟文档?.xPath("//ul[@class='attribution']")?.first
print("作者(Author): \(String(describing: attribution?.content))") // 作者(Author): Optional("ByHonghao Zhang")

许可证

The MIT License (MIT)

Copyright (c) 2019 Honghao Zhang (张宏昊)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.