1. 程式人生 > >[Swift通天遁地]七、數據與安全-(2)使用Fuzi(斧子)類庫實現對XML和HTML文檔的快速解析

[Swift通天遁地]七、數據與安全-(2)使用Fuzi(斧子)類庫實現對XML和HTML文檔的快速解析

contents 控制 元素 frame uikit works 配置文件 cocoapods over

本文將演示使用Fuzi(斧子)類庫實現對XML和HTML文檔的快速解析。

首先確保在項目中已經安裝了所需的第三方庫。

點擊【Podfile】,查看安裝配置文件。

1 platform :ios, 12.0
2 use_frameworks!
3 
4 target DemoApp do
5     source https://github.com/CocoaPods/Specs.git
6     pod Fuzi
7 end

根據配置文件中的相關配置,安裝第三方庫。

然後點擊打開【DemoApp.xcworkspace】項目文件。

在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】

  1 import UIKit
  2 //引入已經安裝的第三方類庫
  3 import Fuzi
  4 
  5 class ViewController: UIViewController {
  6     
  7     override func viewDidLoad() {
  8         super.viewDidLoad()
  9         // Do any additional setup after loading the view, typically from a nib.
 10         //實現對XML文檔的解析
 11
parseXML() 12 //實現對HTML文檔的解析 13 parseHTML() 14 } 15 16 //添加一個方法,實現對XML文檔的解析 17 func parseXML() 18 { 19 //從項目中讀取指定文件名稱的待解析的文檔。 20 let fileUrl = URL(fileURLWithPath: ((#file as NSString).deletingLastPathComponent as NSString).appendingPathComponent("
nutrition.xml")) 21 //添加一個異常捕捉語句,用來處理對XML文檔的解析操作。 22 do 23 { 24 //讀取待解析的文檔,並存儲在一個數據常量中。 25 let data = try Data(contentsOf: fileUrl) 26 //將加載的數據轉換成文檔對象。 27 let document = try XMLDocument(data: data) 28 29 //獲得文檔對象的根節點,並在控制臺輸出根節點的標簽的值。 30 if let root = document.root 31 { 32 //輸出日誌信息 33 print("Root Element: \(String(describing: root.tag))") 34 35 print("\nDaily values:") 36 //獲得根節點中指定標簽的節點, 37 //然後獲得該節點下的所有子節點, 38 //並對子節點進行遍歷。 39 for element in root.firstChild(tag: "daily-values")?.children ?? [] 40 { 41 //獲得節點標簽的名稱 42 let nutrient = element.tag 43 //獲得節點的值 44 let amount = element.numberValue 45 //獲得節點屬性的值 46 let unit = element["units"] 47 //在控制臺輸出節點各元素的值 48 print("- \(amount!)\(unit!) \(nutrient!)") 49 } 50 //輸出一個換行符 51 print("\n") 52 53 //通過X路徑格式,獲得指定的節點。 54 //這裏獲得食物節點下的名稱子節點。 55 var xpath = "//food/name" 56 //在控制臺輸出該節點。 57 print("XPath Search: \(xpath)") 58 59 //遍歷所有的名稱節點 60 for element in document.xpath(xpath) 61 { 62 //並在控制臺輸出食物的名稱 63 print("\(element)") 64 } 65 //輸出另一個換行符 66 print("\n") 67 68 //還可以通過和CSS相似的方法,來查找指定的節點。 69 //這裏獲得食物節點下的指定的子節點。 70 let css = "food > serving[units]" 71 //初始化一個文檔元素 72 var blockElement:XMLElement? = nil 73 //在控制臺輸出搜索路徑 74 print("CSS Search: \(css)") 75 76 //對指定路徑下的節點進行遍歷 77 for (index, element) in document.css(css).enumerated() 78 { 79 //當循環的索引為數字1時, 80 if index == 1 81 { 82 //獲得遍歷的節點。 83 blockElement = element 84 break 85 } 86 } 87 //然後在控制臺輸出第二個節點。 88 print("Second element: \(blockElement!)\n") 89 90 //在控制臺輸出食物節點下的,名稱子節點的X路徑。 91 xpath = "//food/name" 92 print("XPath Search: \(xpath)") 93 94 //獲得文檔中的指定路徑的第一個節點, 95 //在控制臺輸出該節點的內容。 96 let firstElement = document.firstChild(xpath: xpath)! 97 print("First element: \(firstElement)") 98 } 99 } 100 catch 101 { 102 print("Something went wrong :(") 103 } 104 } 105 106 //添加一個方法,實現對HTML文檔的解析 107 func parseHTML() 108 { 109 //從項目中讀取指定的網頁文件 110 let fileUrl = URL(fileURLWithPath: ((#file as NSString).deletingLastPathComponent as NSString).appendingPathComponent("index.html")) 111 //添加一個異常捕捉語句,用來處理對HTML文檔的解析操作。 112 do 113 { 114 //獲得指定位置的網頁文件,將文件存儲在數據對象中。 115 let data = try Data(contentsOf: fileUrl) 116 //將網頁數據轉換成文檔對象。 117 let doc = try HTMLDocument(data: data) 118 119 //獲得文檔對象中的第一個指定的節點, 120 if let elementById = doc.firstChild(css: "#copyright") 121 { 122 //並在控制臺輸出該節點的內容。 123 print(elementById.stringValue) 124 } 125 126 //獲得文檔對象中的所有鏈接節點, 127 //並對鏈接節點進行遍歷 128 for link in doc.css("a, link") 129 { 130 //在控制臺輸出鏈接的路徑 131 print(link.rawXML) 132 print(link["href"] as Any) 133 } 134 135 //獲得主體節點下的所有鏈接節點, 136 if let firstAnchor = doc.firstChild(xpath: "//body/a") 137 { 138 //並輸出第一個網絡鏈接。 139 print(firstAnchor["href"] as Any) 140 } 141 142 //獲得頭結點下的所有腳本節點, 143 for script in doc.xpath("//head/script") 144 { 145 //並輸出腳本節點的文檔路徑 146 print(script["src"] ?? "") 147 } 148 149 //獲得主體節點下的所有鏈接節點, 150 if let result = doc.eval(xpath: "count(//body/a)") 151 { 152 //並在控制臺輸出該數量 153 print("anchor count : \(result.doubleValue)") 154 } 155 156 //輸出標題 157 print(doc.title as Any) 158 //輸出頭節點 159 print(doc.head as Any) 160 //輸出主題節點 161 print(doc.body as Any) 162 } 163 catch 164 { 165 print("Something went wrong :(") 166 } 167 } 168 169 override func didReceiveMemoryWarning() { 170 super.didReceiveMemoryWarning() 171 // Dispose of any resources that can be recreated. 172 } 173 }

[Swift通天遁地]七、數據與安全-(2)使用Fuzi(斧子)類庫實現對XML和HTML文檔的快速解析