Swiftpack.co - Package - honghaoz/Ji

Ji: a Swift XML/HTML parser

Ji 戟

CI Status CocoaPods Version Carthage Compatible License Platform

Ji (戟) is a Swift wrapper on libxml2 for parsing XML/HTML.

Features

  • ☑ Build XML/HTML Tree and Navigate.
  • ☑ XPath Query Supported.
  • ☑ Comprehensive Unit Test Coverage.
  • ☑ Support Swift Package Manager (SPM). Linux compatible.

Requirements

  • iOS 8.0+ / Mac OS X 10.9+ / watchOS 2.0+ / tvOS 9.0+
  • Xcode 8.0+

Installation

CocoaPods

To integrate Ji into your Xcode project using CocoaPods, specify it in your Podfile:

use_frameworks!

pod 'Ji', '~> 5.0.0'

Then, run the following command:

$ pod install

Carthage

To integrate Ji into your Xcode project using Carthage, specify it in your Cartfile:

github "honghaoz/Ji" ~> 5.0.0

Swift Package Manager (SPM)

Prerequisites

  • OSX
brew install libxml2
brew link --force libxml2
  • Linux
$ sudo apt-get install libxml2-dev

Update Package.swift

To integrate Ji in your project, add the proper description to your Package.swift file:

// 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"]
        ),
        ...
    ]
)

Manually

If you prefer not to use a dependency manager, you can integrate Ji into your project manually.

  • Add sources into your project:

    • Drag Ji.swift, JiHelper.swift and JiNode.swift in Sources/Ji folder into your project.
    • Drag Sources/Clibxml2 folder into your project.
  • Configure your project:

    • Open project, select the target, under Build Settings, in Header Search Paths, add $(SDKROOT)/usr/include/libxml2
    • Under Build Settings, in Import Paths, add $(SRCROOT)/Clibxml2 (Make sure this is the path to the Clibxml2 folder)

Usage

If you are using CocoaPods to integrate Ji. Import Ji first:

import Ji
  • Init with URL:
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")
  • 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 jiDoc = Ji(xmlString: xmlString)
let bodyNode = jiDoc?.rootNode?.firstChildWithName("body")
print("body: \(String(describing: bodyNode?.content))") // body: Optional("Don\'t forget me this weekend!")
  • Init with Data:
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/pods/Ji")!)
let attribution = 戟文档?.xPath("//ul[@class='attribution']")?.first
print("作者(Author): \(String(describing: attribution?.content))") // 作者(Author): Optional("ByHonghao Zhang")

License

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.

Github

link
Stars: 821

Dependencies

Releases

Support Swift 5.1 - 2020-01-15 05:53:15

Update SPM to Swift 5.1

Support Swift 5.0 - 2019-06-09 08:15:41

Support Swift 4.2 - 2019-06-09 07:52:37

Swift 4.0 - 2017-10-06 05:47:36

Support Swift 4.0

Support for SPM (Swift Package Manager) - 2016-11-28 22:36:56

  • Support for Linux
  • Support for Swift Package Manager

Swift 3 - 2016-10-14 04:07:39

Supported Swift 3 with Xcode 8

Swift 2.3 - 2016-09-27 04:21:41

Updated to support Swift 2.3 with Xcode 8

watchOS and tvOS supported - 2015-10-22 15:45:14

  • watchOS 2.0+ Supported
  • tvOS 9.0+ Supported
  • Internal updates
    • Cleaner targets
    • Xcode 7.1 importing issues fixed

Swift 2.0 Supports - 2015-09-17 15:17:21

Swift 2.0 :tada: Other bugs fix

Carthage Support - 2015-08-28 02:17:04

  • Carthage now supported.
  • Project and directories are reorganized.
  • added type alias 戟 = Ji and 戟节点 = JiNode

'rawContent' and 'rawValue' updated, Printable conforms - 2015-08-15 16:21:49

  • rawContent updated:
    • rawContent will include tags and children.
  • content will keep original content text, include leading and trailing white spaces/newlines.
  • rawValue has been removed.
  • value will keep original value text, include leading and trailing white spaces/newlines.
  • Printable protocol conforms.
    • You can print out Ji document and Ji node, e.g. println("\(rootNode)").

Framework supporting files added - 2015-08-10 14:54:25

  • Added Framework supporting files
  • Cleaned Source folder
    • Moved Ji-libxml headers out
    • Removed 'Ji-Bridging-Header.h'

First release - 2015-08-03 23:03:22

  • Build an XML/HTML tree and navigate the tree
  • Evaluate XPath expression and get results nodes
  • Cocoapods integrated