zachwaugh / Kanna

Kanna(鉋) is an XML/HTML parser for macOS / iOS / tvOS.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Kanna(鉋)

Kanna(鉋) is an XML/HTML parser for Mac OS X and iOS. (It was formerly known as Swift-HTML-Parser)

It was inspired by Nokogiri(鋸).

Build Status License Platform Language Issues Cocoapod Carthage compatible Reference Status

ℹ️ Documentation

Features:

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

Installation:

Swift 3.x (Beta)

Open terminal.app and execute sudo xcode-select -switch /Applications/Xcode-beta.app/Contents/Developer

#####Carthage Adding it to your Cartfile:

github "tid-kijyun/Kanna" "swift3.0"
  1. In the project settings add $(SDKROOT)/usr/include/libxml2 to the "header search paths" field

Swift 2.x

Three means of installation are supported:

#####CocoaPods ⚠️ CocoaPods (0.39 or later) is required.

Adding it to your Podfile:

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

#####Carthage Adding it to your Cartfile:

github "tid-kijyun/Kanna" ~> 1.1.0

#####Manual Installation

  1. Add these files to your project:
    Kanna.swift
    CSS.swift
    libxmlHTMLDocument.swift
    libxmlHTMLNode.swift
    libxmlParserOption.swift
  2. In the target settings add $(SDKROOT)/usr/include/libxml2 to the Search Paths > Header Search Paths field
  3. In the target settings add -lxml2 to the Linking > Other Linker Flags field
  4. Import libxml headers:

Copy the those import statements:

#import <libxml/HTMLtree.h>
#import <libxml/xpath.h>
#import <libxml/xpathInternals.h>

and paste them into your [Modulename]-Bridging-Header.h

Note: With manual installation, this library doesn't need to be imported, or namespace-qualified in your code.

Synopsis:

import Kanna

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

if let doc = Kanna.HTML(html: html, encoding: NSUTF8StringEncoding) {
    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 = Kanna.XML(xml: xml, encoding: NSUTF8StringEncoding) {
    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 infomation.

About

Kanna(鉋) is an XML/HTML parser for macOS / iOS / tvOS.

License:MIT License


Languages

Language:C 79.8%Language:Swift 18.2%Language:HTML 0.8%Language:C++ 0.5%Language:Objective-C 0.5%Language:Ruby 0.2%