[Swift通天遁地]7、数据与安全-(1)XML文档的建立和解析

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-bfduphmd-ma.html 
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

目录:[Swift]通天遁地Swiftios

本文将演示可扩展标记语言文档的解析和编写。git

首先确保在项目中已经安装了所需的第三方库。github

点击【Podfile】,查看安装配置文件。swift

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

根据配置文件中的相关配置,安装第三方库。数组

而后点击打开【DemoApp.xcworkspace】项目文件。安全

在项目导航区,打开视图控制器的代码文件【ViewController.swift】微信

  1 import UIKit
  2 //引入已经安装的第三方类库
  3 import AEXML
  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         //读取和解析文档
 11         readXML()
 12         //编写XML文档
 13         writeXML()
 14     }
 15     
 16     //添加一个方法,用来读取和解析文档
 17     func readXML()
 18     {
 19         //读取项目中的待解析的文档
 20         guard let xmlPath = Bundle.main.path(forResource: "XMLExample", ofType: "xml"),
 21             let data = try? Data(contentsOf: URL(fileURLWithPath: xmlPath)) else {
 22             return
 23         }
 24         
 25         //添加一个异常捕捉语句,用来实现对文档的解析
 26         do
 27         {
 28             //初始化一个文档对象,使用该文档对象,存储和加载数据。
 29             let xmlDoc = try AEXMLDocument(xml: data, options: AEXMLOptions())
 30             print(xmlDoc.xml)
 31             
 32             print("-----------------------------")
 33             //经过一个循环语句,遍历根节点中的全部子节点。            
 34             for child in xmlDoc.root.children
 35             {
 36                 //并在控制台输出子节点的名称
 37                 print("child.name:\(child.name)")
 38             }
 39             
 40             print("-----------------------------")
 41             //从根节点中得到cats子节点,
 42             //而后再得到该节点中的cat子节点,
 43             //接着输出该节点的值。
 44             print(xmlDoc.root["cats"]["cat"].value as Any)
 45             //得到并初始节点的详细内容
 46             print(xmlDoc.root["cats"]["cat"].string)
 47             //从根节点中得到dogs子节点,
 48             //而后再得到该节点中的dog子节点,
 49             //接着输出该节点的值。
 50             print(xmlDoc.root["dogs"]["dog"].last?.value as Any)
 51             //得到该节点中的第三个节点,并输出该节点的详细内容。
 52             print(xmlDoc.root["dogs"].children[2].string)
 53             
 54             print("--------------all---------------")
 55             //得到cats节点下的全部指定的子节点,并存储在一个数组中。
 56             if let cats = xmlDoc.root["cats"]["cat"].all
 57             {
 58                 //对由子节点组成的数组进行遍历
 59                 for cat in cats
 60                 {
 61                     //而后输出遍历到的元素的值
 62                     if let name = cat.value
 63                     {
 64                         print(name)
 65                     }
 66                 }
 67             }
 68             
 69             //过滤cats节点下的全部指定的子节点            
 70             print("---------------withValue--------------")
 71             //并得到拥有特定值的cat节点
 72             if let cats = xmlDoc.root["cats"]["cat"].all(withValue: "Tinna")
 73             {
 74                 //对由子节点组成的数组进行遍历
 75                 for cat in cats
 76                 {
 77                     //并输出节点的详细内容
 78                     print(cat.string)
 79                 }
 80             }
 81             
 82             print("-------------withAttributes----------------")
 83             //过滤cats节点下的全部指定的子节点,并得到拥有特定属性的子节点。
 84             if let cats = xmlDoc.root["cats"]["cat"].all(withAttributes: ["breed" : "Domestic", "color" : "yellow"])
 85             {
 86                 //对由子节点组成的数组进行遍历,
 87                 for cat in cats
 88                 {
 89                     //并输出节点的详细内容
 90                     print(cat.string)
 91                 }
 92             }
 93             
 94             print("--------------attributes---------------")
 95             //对dogs节点下的全部子节点,进行遍历操做。
 96             for dog in xmlDoc.root["dogs"]["dog"].all!
 97             {
 98                 //得到该节点的颜色属性
 99                 if let color = dog.attributes["color"]
100                 {
101                     //若是颜色属性的值为白色
102                     if color == "white"
103                     {
104                         //则输出当前颜色的详细内容
105                         print(dog.string)
106                     }
107                 }
108             }
109             
110             print("-----------------------------")
111             //经过count属性,能够得到同名节点的数量。
112             print(xmlDoc.root["cats"]["cat"].count)
113             //并输出指定节点的品牌属性
114             print(xmlDoc.root["cats"]["cat"].attributes["breed"]!)
115             //得到并输出指定节点的标签内容,
116             //该内容中的回车符和将被去除
117             print(xmlDoc.root["cats"]["cat"].xmlCompact)
118         }
119         catch
120         {
121             //输出错误信息
122             print("\(error)")
123         }
124     }
125     
126     //添加一个方法,用来编写XML文档
127     func writeXML()
128     {
129         //初始化一个文档对象
130         let soapRequest = AEXMLDocument()
131         //建立一个字典对象,做为文档的命名空间
132         let attributes = ["xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance",
133                           "xmlns:xsd" : "http://www.w3.org/2001/XMLSchema"]
134         
135         //往文档中添加一个指定名称和属性的节点
136         let envelope = soapRequest.addChild(name: "soap:Envelope", attributes: attributes)
137         //继续往该节点添加一个名为"Employees"的节点
138         let employees = envelope.addChild(name: "Employees")
139         //往"Employees"子节点添加一个名为"Managers"的节点
140         let managers = employees.addChild(name: "Managers")
141         //往"Managers"节点添加一个指定名称和年龄等属性的子节点
142         managers.addChild(name: "Manager", attributes:["Name":"Jerry", "age":"40"])
143         //往"Managers"节点添加一个指定名称和年龄等属性的子节点
144         managers.addChild(name: "Manager", attributes:["Name":"Peter", "age":"44"])
145         
146         //往"Employees"子节点添加一个名为"Engineers"的节点
147         let engineers = employees.addChild(name: "Engineers")
148         //往"Engineers"节点添加一个指定名称和年龄等属性的子节点
149         engineers.addChild(name: "Engineer", attributes:["Name":"Bill", "age":"29"])
150         engineers.addChild(name: "Engineer", attributes:["Name":"Somus", "age":"31"])
151         
152         print(soapRequest.xml)
153     }
154     
155     override func didReceiveMemoryWarning() {
156         super.didReceiveMemoryWarning()
157         // Dispose of any resources that can be recreated.
158     }
159 }
相关文章
相关标签/搜索