在 Swift 4 IOS 中将 XML 数据转换为字符串 [英] Get XML data into String in Swift 4 IOS

查看:51
本文介绍了在 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

这篇关于在 Swift 4 IOS 中将 XML 数据转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆