How to generate xml in Swift

Issue #556

Instead of learning XMLParser, we can make a lightweight version

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import Foundation

public protocol XmlItem {
func toLines() -> [String]
}

public struct XmlString: XmlItem {
public let key: String
public let value: String

public init(key: String, value: String) {
self.key = key
self.value = value
}

public func toLines() -> [String] {
return [
"<key>\(key)</key>",
"<string>\(value)</string>"
] as [String]
}
}

public struct XmlBool: XmlItem {
public let key: String
public let value: Bool

public init(key: String, value: Bool) {
self.key = key
self.value = value
}

public func toLines() -> [String] {
let string = value ? "<true/>" : "<false/>"
return [
"<key>\(key)</key>",
"\(string)"
] as [String]
}
}

public struct XmlDict: XmlItem {
public let key: String
public let items: [XmlItem]

public init(key: String, items: [XmlItem]) {
self.key = key
self.items = items
}

public func toLines() -> [String] {
var lines = [String]()
lines.append("<dict>")
lines.append(contentsOf: items.flatMap({ $0.toLines() }))
lines.append("</dict>")

return lines
}
}

public class XmlGenerator {
public init() {}
public func generateXml(_ items: [XmlItem]) -> String {
let content = items.flatMap({ $0.toLines() }).joined(separator: "\n")
let xml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
\(content)
</dict>
</plist>
"""
return xml
}

public func xmlItems(dictionary: [String: Any]) -> [XmlItem] {
return dictionary.flatMap({ (key, value) -> [XmlItem] in
switch value {
case let string as String:
return [XmlString(key: key, value: string)]
case let bool as Bool:
return [XmlBool(key: key, value: bool)]
case let nestedDictionary as [String: Any]:
return xmlItems(dictionary: nestedDictionary)
default:
return []
}
})
}
}

Comments