Ji框架的地址:https://github.com/honghaoz/Ji
pod 'Ji', '~> 2.0.1'
import UIKit
class ViewController: UIViewController {
overridefunc viewDidLoad() {
super.viewDidLoad()
// Init with data
let googleIndexData =try? Data(contentsOf:URL(string: "http://www.google.com")!)
iflet googleIndexData = googleIndexData {
let jiDoc =Ji(htmlData: googleIndexData)!
let htmlNode = jiDoc.rootNode!
print("html tagName:\(htmlNode.tagName)")
let aNodes = jiDoc.xPath("//body//a")
iflet firstANode = aNodes?.first {
print("first a node tagName:\(firstANode.name)")
let href = firstANode["href"]
print("first a node href:\(href)")
}
} else {
print("google.com is inaccessible")
}
print("")
// Init with URL
let jiAppleSupportDoc =Ji(htmlURL: URL(string:"http://www.apple.com/support")!)
let titleNode = jiAppleSupportDoc?.xPath("//head/title")?.first
print("title:\(titleNode?.content)")
print("")
// Init with String
let xmlString ="<?xml version='1.0' encoding='UTF-8'?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>"
let xmlDoc =Ji(xmlString: xmlString)
let bodyNode = xmlDoc?.rootNode?.firstChildWithName("body")
print("body:\(bodyNode?.content)")
print("")
// Just for fun
let戟文档 =戟(htmlURL:URL(string: "https://cocoapods.org/pods/Ji")!)
let attribution =戟文档?.xPath("//ul[@class='attribution']")?.first
print("作者(Author):\(attribution?.content)")
}
}
html tagName: Optional("html")
first a node tagName: Optional("a")
first a node href: Optional("http://www.google.co.jp/imghp?hl=zh-CN&tab=wi")
title: Optional("Official Apple Support")
body: Optional("Don\'t forget me this weekend!")
作者(Author): Optional("ByHonghao Zhang")