Kanna(鉋) is an XML/HTML parser for cross-platform(macOS, iOS, tvOS, watchOS and Linux!).
It was inspired by Nokogiri(鋸).
- XPath 1.0 support for document searching
- CSS3 selector support for document searching
- Support for namespaces
- Comprehensive test suite
If you want to use Swift 4, please use branch feature/v4.0.0
.
This is a beta version. There may be API changes.
1.1.0 or later
) is required.
Adding it to your Podfile
:
use_frameworks!
pod 'Kanna', '~> 2.1.0'
Adding it to your Cartfile
:
github "tid-kijyun/Kanna" ~> 2.1.0
- In the project settings add
$(SDKROOT)/usr/include/libxml2
to the "header search paths" field
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
- Add these files to your project:
Kanna.swift
CSS.swift
libxmlHTMLDocument.swift
libxmlHTMLNode.swift
libxmlParserOption.swift
Modules - In the target settings add
$(SDKROOT)/usr/include/libxml2
to theSearch Paths > Header Search Paths
field - In the target settings add
$(SRCROOT)/Modules
to theSwift Compiler - Search Paths > Import Paths
field
Three means of installation are supported:
0.39 or later
) is required.
Adding it to your Podfile
:
use_frameworks!
pod 'Kanna', '~> 1.1.0'
Adding it to your Cartfile
:
github "tid-kijyun/Kanna" ~> 1.1.0
- Add these files to your project:
Kanna.swift
CSS.swift
libxmlHTMLDocument.swift
libxmlHTMLNode.swift
libxmlParserOption.swift - In the target settings add
$(SDKROOT)/usr/include/libxml2
to theSearch Paths > Header Search Paths
field - In the target settings add
-lxml2
to theLinking > Other Linker Flags
field - 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.
import Kanna
let html = "<html>...</html>"
if let doc = 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 = 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)
}
}
The MIT License. See the LICENSE file for more infomation.