Swiftpack.co - Package - tid-kijyun/Kanna

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 for Swift 5

CocoaPods

Add the following to your Podfile:

use_frameworks!
pod 'Kanna', '~> 5.2.2'

Carthage

Add the following to your Cartfile:

github "tid-kijyun/Kanna" ~> 5.2.2

For xcode 11.3 and earlier, the following settings are required.

  1. In the project settings add $(SDKROOT)/usr/include/libxml2 to the "header search paths" field

Swift Package Manager

  1. Installing libxml2 to your computer:
// macOS: For xcode 11.3 and earlier, the following settings are required.
$ brew install libxml2
$ brew link --force libxml2

// Linux(Ubuntu):
$ sudo apt-get install libxml2-dev
  1. Add the following to your Package.swift:
// swift-tools-version:5.0
import PackageDescription

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

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

// Linux(Ubuntu)
$ 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

Installation for swift 4

Installation for swift 3

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)
    }
}

Donation

If you like Kanna, please donate via GitHub sponsors or PayPal.
It is used to improve and maintain the library.

License

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

Github

link
Stars: 2071

Dependencies

Used By

Total: 0

Releases

Support for Xcode 12 - 2020-10-11 22:07:31

  • Adapt Package.swift for Xcode 12 #241 @djbe

Minor bug fix - 2020-03-28 16:31:20

  • fix CSS selector group for non-root nodes #218 @kewlbear
  • Fixed not to display libxml2 related warning in SwiftPM of Xcode 11.4. (pkgConfig) #231

hotfix - 2020-03-26 06:47:08

  • Fixed a bug that HTML cannot be parsed correctly. #232
  • Fixed not to display libxml2 related warning in SwiftPM of Xcode 11.4. #231

Support for Swift 5.2 - 2020-03-25 06:50:08

⚠️ This version has a problem with parsing HTML. Please use 5.2.1 or later. Added support for Swift 5.2 (Xcode 11.4). This version supports Swift 5.0, 5.1 and 5.2.

Swift Package Manager

  • We no longer need to install libxml2 using brew on Xcode 11.4 or later on macOS.

Support for Swift 5 - 2019-04-01 16:16:37

There is no change other than Swift 5 support. If you are using Swift 4, use version 4.x.

Support for Swift 4.2 - 2019-01-09 07:23:18

- 2018-09-11 07:54:56

Fixed a bug. #198

4.0.1 - 2018-08-11 11:27:51

Support for Swift 4.1

4.0.0 - 2018-03-04 08:11:31

Support for Swift 4

- 2017-04-01 21:44:27

Support for Swift 3.1

For Linux: This release is incompatible with Swift 3.0.1 or earlier.

- 2017-03-12 11:57:11

2.1.1 - 2016-12-07 06:09:21

Fixed MACOSX_DEPLOYMENT_TARGET from 10.12 to 10.9.

Support for SwiftSPM - 2016-12-05 17:08:33

- 2016-11-02 15:02:57

#117

- 2016-10-28 13:34:11

Swift Package Manager

swift build -Xswiftc -DSPM

Support for linux (beta) - 2016-10-04 15:24:08

Support for Swift3.0 - 2016-09-21 04:04:22

Added support for Swift2.3 - 2016-09-02 23:37:04

1.1.0 - 2016-06-28 10:18:16

New feature

  • Added support for XPathObject #78
  • Changing text contents #30
  • Moving nodes

Bug fix

  • Removed a module map. #75
  • Fixed XML Memory Leak #74

1.0.6: Swift 2.2 supports - 2016-03-22 03:29:27

1.0.5: Added carthage bit code support - 2016-02-21 17:47:44

Thanks @taketin.

1.0.4 - 2015-12-05 10:42:28

1.0.3 - 2015-11-30 16:03:32

Added support for modulemap

1.0.2 - 2015-10-22 12:26:51

Added support tvOS and watchOS.

1.0.1 - 2015-10-22 11:36:20

Fixed error Xcode7.1 with cocoapods

1.0.0 - 2015-09-12 04:12:42

Support for Swift2

0.1.5 - 2015-09-03 19:51:34

Fixed #32

0.1.4 - 2015-09-01 20:00:21

0.1.3 - 2015-08-20 12:51:22

Fixed memory leak

0.1.2 - 2015-07-28 11:26:05

fixed podspec file.