都9102年了,如果遇到发送数据还需要用xml的时候,对于习惯了json的小伙伴实在是不太友好,所以写了简单的Dictionary转xml的方法。
使用extension对其扩展:
extension Dictionary {
//The outermost should be the root node, otherwise it violates the xml syntax specification
func xmlString() -> String {
let json = JSON(self)
var xml = ""
for key in json.dictionaryValue.keys {
let type = json[key].type
var value = ""
if type == .array {
let obj = json[key].arrayObject
value = arrayToXmlString(key, obj ?? [Any]())
} else if type == .dictionary {
let obj = json[key].dictionaryObject
value = obj?.xmlString() ?? ""
} else {