Git Product home page Git Product logo

kanna's Introduction

Kanna(鉋)

Kanna(鉋) is an XML/HTML parser for MacOSX/iOS. (formerly Swift-HTML-Parser)

It was inspired by Nokogiri(鋸).

License Platform Language Issues Cocoapod Carthage compatible Reference Status

Features:

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

Installation:

Swift2

#####Cocoapods Adding it to your Podfile:

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

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

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

#####Munually

  1. Add files to your project:
    Kanna.swift
    CSS.swift
    libxmlHTMLDocument.swift
    libxmlHTMLNode.swift
    libxmlParserOption.swift
  2. Copy folder to your project:
    Modules
  3. In the project settings add $(SRCROOT)/YOUR_PROJECT/Modules to the "Swift Compiler - Search Paths > Import Paths" field
  4. In the project settings add $(SDKROOT)/usr/include/libxml2 to the "header search paths" field
  5. In the project settings add -lxml2 to the "Linking > Other Linker Flags" field

Note: For manually, this library don't need import and namespace in your code.

Swift1.2(deprecated)

#####Cocoapods Adding it to your Podfile:

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

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

github "tid-kijyun/Kanna" ~> 0.1.5

Synopsis:

import Kanna

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

if let doc = Kanna.HTML(html: html, encoding: NSUTF8StringEncoding) {
    println(doc.title)
    
    // Search for nodes by CSS
    for link in doc.css("a, link") {
        println(link.text)
        println(link["href"])
    }
    
    // Search for nodes by XPath
    for link in doc.xpath("//a | //link") {
        println(link.text)
        println(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) {
        println(author.text)
    }
}

Migration guide: (from Swift-HTML-Parser)

Initialize

// Swift-HTML-Parser
var err: NSError?
var parser = HTMLParser(html: html, error: &err)

// New: Kanna
if let doc = Kanna.HTML(html: html, encoding: NSUTF8StringEncoding) {
}

Search for node

Search for nodes by tag

// Swift-HTML-Parser
if let nodes = parser.body?.findChildTags("div") {
    for node in nodes {
        println(node.contents)
    }
}

// NEW: Kanna
for node in doc.css("div") {
    println(doc.text)
}

Search for nodes by XPath

// Swift-HTML-Parser
if let nodes = parser.body?.xpath("//div") {
    for node in nodes {
        println(node.contents)
    }
}

// New: Kanna
for node in doc.xpath("//div") {
    println(node.text)
}

Search for nodes by CSS

// Swift-HTML-Parser
if let nodes = parser.body?.css("li:nth-child(2n)") {
    for node in nodes {
        println(node.contents)
    }
}

// New: Kanna
for node in doc.css("li:nth-child(2n)") {
    print(node.text)
}

Search for first node

// Swift-HTML-Parser
if let node = parser.body?.findChildTag("div") {
    println(node.contents)
}

// New: Kanna
if let node = doc.at_css("div") {
    println(node.text)
}

Get contents

Get contents

// Swift-HTML-Parser
node.contents

// New: Kanna
node.text

Get attribute

// Swift-HTML-Parser
node.getAttributeNamed("href")

// New: Kanna
node["href"]

Get raw contents

// Swift-HTML-Parser
node.rawContents

// New: Kanna
node.innerHTML

Lisense:

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

kanna's People

Contributors

dodderjs avatar leoru avatar phimage avatar stigi avatar tid-kijyun avatar xpqz avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.