Kanna

Kanna(鉋) is an XML/HTML parser for Swift.

Github星跟蹤圖

Kanna(鉋)

Kanna(鉋) is an XML/HTML parser for cross-platform(macOS, iOS, tvOS, watchOS and Linux!).

It was inspired by Nokogiri(鋸).

Build Status
Platform
Cocoapod
Carthage compatible
Swift Package Manager
Reference Status

:information_source: Documentation

Features:

  • XPath 1.0 support for document searching
  • CSS3 selector support for document searching
  • Support for namespaces
  • Comprehensive test suite

Installation:

Swift 5

CocoaPods

Adding it to your Podfile:

use_frameworks!
pod 'Kanna', '~> 5.0.0'
Carthage

Adding it to your Cartfile:

github "tid-kijyun/Kanna" ~> 5.0.0
  1. In the project settings add $(SDKROOT)/usr/include/libxml2 to the "header search paths" field
Others(Swift PM and manual Installation)

Please refer to the case of Swift 4.

Swift 4

CocoaPods

:warning: CocoaPods (1.1.0 or later) is required.

Adding it to your Podfile:

use_frameworks!
pod 'Kanna', '~> 4.0.0'
Carthage

Adding it to your Cartfile:

github "tid-kijyun/Kanna" ~> 4.0.0
  1. In the project settings add $(SDKROOT)/usr/include/libxml2 to the "header search paths" field
Swift Package Manager

Installing libxml2 to your computer:

// macOS
$ brew install libxml2
$ brew link --force libxml2

// Linux(Ubuntu)
$ sudo apt-get install libxml2-dev

Adding it to your Package.swift:

// swift-tools-version:4.0
import PackageDescription

let package = Package(
    name: "YourProject",
    dependencies: [
        .package(url: "https://github.com/tid-kijyun/Kanna.git", from: "4.0.0")
    ],
    targets: [
        .target(
            name: "YourTarget",
            dependencies: ["Kanna"]),
    ]
)
$ swift build

Note: When a build error occurs, please try run the following command:

$ sudo apt-get install pkg-config
Manual Installation
  1. Add these files to your project:
    Kanna.swift
    CSS.swift
    libxmlHTMLDocument.swift
    libxmlHTMLNode.swift
    libxmlParserOption.swift
    Modules
  2. In the target settings add $(SDKROOT)/usr/include/libxml2 to the Search Paths > Header Search Paths field
  3. In the target settings add $(SRCROOT)/Modules to the Swift Compiler - Search Paths > Import Paths field

Swift 3.0

For now, please use the feature/v3.0.0 branch.

CocoaPods

:warning: CocoaPods (1.1.0 or later) is required.

Adding it to your Podfile:

use_frameworks!
pod 'Kanna', :git => 'https://github.com/tid-kijyun/Kanna', :branch => 'feature/v3.0.0'
Carthage

Adding it to your Cartfile:

github "tid-kijyun/Kanna" "feature/v3.0.0"
  1. In the project settings add $(SDKROOT)/usr/include/libxml2 to the "header search paths" field
Swift Package Manager

Installing libxml2 to your computer:

// macOS
$ brew install libxml2
$ brew link --force libxml2

// Linux(Ubuntu)
$ sudo apt-get install libxml2-dev

Adding it to your Package.swift:

import PackageDescription

let package = Package(
    name: "YourProject",
    
    dependencies: [
        .Package(url: "https://github.com/tid-kijyun/Kanna.git", majorVersion: 2)
    ]
)
$ swift build

Note: When a build error occurs, please try run the following command:

$ sudo apt-get install pkg-config
Manual Installation
  1. Add these files to your project:
    Kanna.swift
    CSS.swift
    libxmlHTMLDocument.swift
    libxmlHTMLNode.swift
    libxmlParserOption.swift
    Modules
  2. In the target settings add $(SDKROOT)/usr/include/libxml2 to the Search Paths > Header Search Paths field
  3. In the target settings add $(SRCROOT)/Modules to the Swift Compiler - Search Paths > Import Paths field

Synopsis:

import Kanna

let html = "<html>...</html>"

if let doc = try? HTML(html: html, encoding: .utf8) {
    print(doc.title)
    
    // Search for nodes by CSS
    for link in doc.css("a, link") {
        print(link.text)
        print(link["href"])
    }
    
    // Search for nodes by XPath
    for link in doc.xpath("//a, //link") {
        print(link.text)
        print(link["href"])
    }
}
let xml = "..."
if let doc = try? Kanna.XML(xml: xml, encoding: .utf8) {
    let namespaces = [
                    "o":  "urn:schemas-microsoft-com:office:office",
                    "ss": "urn:schemas-microsoft-com:office:spreadsheet"
                ]
    if let author = doc.at_xpath("//o:Author", namespaces: namespaces) {
        print(author.text)
    }
}

License:

The MIT License. See the LICENSE file for more information.

主要指標

概覽
名稱與所有者tid-kijyun/Kanna
主編程語言Swift
編程語言Swift (語言數: 5)
平台
許可證MIT License
所有者活动
創建於2014-06-15 11:14:38
推送於2025-06-26 16:36:54
最后一次提交2025-06-27 01:36:50
發布數41
最新版本名稱6.0.1 (發布於 )
第一版名稱0.0.1 (發布於 )
用户参与
星數2.5k
關注者數43
派生數223
提交數373
已啟用問題?
問題數169
打開的問題數16
拉請求數88
打開的拉請求數2
關閉的拉請求數16
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?