在 Swift 4 IOS 中将 XML 数据转换为字符串
问题描述:
我有一个 XML(字符串格式),我想使用 Swift 4 从它获取单个值到字符串变量.
我的数据如下:
I have an XML (in String format), I want to get individual values from it to string variables using Swift 4.
My data as given below:
let myString ="Adasdnajinasdshabjdbaiusd" //Encrypted Text(Sample)
let MyResult = self.TestObj.decryptData(myString); //Method for Encryption
print(MyResult) // Result in String Format
MyResult 的输出在这里:
The output of MyResult is here:
<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><userDetails><status>success</status><name>Ashique</name><role>admin</role></userDetails>
.
这是字符串格式.那么如何在 Swift 4 中将 Name 和 Role 等这些值输入到字符串变量中?
This is in String format. So How can I get these values like Name and Role in to string variables in Swift 4?
提前致谢!
答
使用 SwiftyXMLParser 它有效..这是吊舱:
Using SwiftyXMLParser it worked.. here is the pod :
pod "SwiftyXMLParser", :git =>'https://github.com/yahoojapan/SwiftyXMLParser.git'
pod "SwiftyXMLParser", :git =>'https://github.com/yahoojapan/SwiftyXMLParser.git'
import SwiftyXMLParser
myString = <userDetails><status>success</status><name>Ashique</name></userDetails>
let xml1 = try! XML.parse(MyString!)
// access xml element
var element1 = xml1["userDetails"]["status"]; // Will Provide result -> success
var element2 = xml1["userDetails"]["name"] // will provide result -> Ashique