XMLParser lets you convert a pure Swift dictionary into XML string and vice versa.
- Swift 2 (Xcode 7+)
- iOS 8+
- ARC
###Cocoa Pods
pod 'XMLParser', '~> 1.0'
####Parsing an XML string from a Dictionary
let body = [
"request" : [
"meta" : [
"type" : "getOrder",
"date" : "2015-08-29 12:00:00",
"device_name" : "iPhone 6 Plus",
"device_os_version" : "iOS 9"
]
],
"encryption" : [
"type" : "RSA"
]
]
let header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
let result = XMLParser.sharedParser.encode(body, header: header)
print(result)
####Result
<?xml version="1.0" encoding="UTF-8"?>
<encryption>
<type>RSA</type>
</encryption>
<request>
<meta>
<type>getOrder</type>
<device_os_version>iOS 9</device_os_version>
<date>2015-08-29 12:00:00</date>
<device_name>iPhone 6 Plus</device_name>
</meta>
</request>
####Associated tags
E.g. <td class='achievements'>0</td>
let data = [
"tr" : [
XMLTag(header: "td", name: "class", value: "num") : 1,
XMLTag(header: "td", name: "class", value: "achievments") : 0,
XMLTag(header: "td", name: "class", value: "sum") : 205
]
]
let result = XMLParser.sharedParser.encode(data)
print(result)
####Result
<tr>
<td class='achievments'>0</td>
<td class='num'>1</td>
<td class='sum'>205</td>
</tr>
==========
####Extracting data from an XML converted string
let convertedString = "<request><meta><type>getOrder</type><date>2015-08-29 12:00:00</date><device_name>iPhone 6 Plus</device_name><device_os_version>iOS 9</device_os_version></meta></request><encryption><type>RSA</type></encryption>"
let result = XMLParser.sharedParser.decode(convertedString)
print(result)
####Result
[
type: [getOrder, RSA],
device_os_version: [iOS 9],
date: [2015-08-29 12:00:00],
device_name: [iPhone 6 Plus]
]
Eugene Mozharovsky (@DottieYottie)