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:

5.0.0′
“>

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:

5.0.0
“>

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:

<div class="highlight highlight-source-swift position-relative" data-snippet-clipboard-copy-content="let xmlString = "ToveJaniReminderDon’t forget me this weekend!"
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 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!")