ios如何解析html XML,Kanna(鉋) 是macOS / iOS / tvOS的一个 XML/HTML 解析器

Kanna(鉋)

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

It was inspired by Nokogiri(鋸).

625332134c6f4d4600884b99daebf603.png625332134c6f4d4600884b99daebf603.png625332134c6f4d4600884b99daebf603.png625332134c6f4d4600884b99daebf603.png625332134c6f4d4600884b99daebf603.png625332134c6f4d4600884b99daebf603.png

Features:

XPath 1.0 support for document searching

CSS3 selector support for document searching

Support for namespaces

Comprehensive test suite

Installation:

Swift 4

CocoaPods

⚠️ 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

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

In the target settings add $(SDKROOT)/usr/include/libxml2 to the Search Paths > Header Search Paths field

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

⚠️ 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"

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

In the target settings add $(SDKROOT)/usr/include/libxml2 to the Search Paths > Header Search Paths field

In the target settings add $(SRCROOT)/Modules to the Swift Compiler - Search Paths > Import 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

In the target settings add $(SDKROOT)/usr/include/libxml2 to the Search Paths > Header Search Paths field

In the target settings add -lxml2 to the Linking > Other Linker Flags field

Import libxml headers:

Copy the those import statements:

#import

#import

#import

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 = "..."

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值