Ben Ben - 3 months ago 33
Swift Question

Write XML in swift on iOS

I'm trying to find a solution for creating a XML Document and put it in a String, I did some research on how to create generate or write xml in swift, but I found only libraries and I don't want to use them, is there a solution?

I have some

UITextFields
, the user put values, when they click on a button, I want to create a xml document.

For exemple:
User put his name and age, when they validate I want to have this in a string:

"<?xml version='1.0' encoding='UTF-8'?><name>Ben</name><age>18</age>"


Code:

@IBOutlet var tfNom: UITextField!
@IBOutlet var tfPrenom: UITextField!


override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.barTintColor = UIColor(red: 38.0/255.0, green: 51.0/255.0, blue: 85.0/255.0, alpha: 1.0)
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Gotham", size: 13)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
self.title = "DEMANDE GRATUITE"
}

@IBAction func Valider(sender: AnyObject) {
verifComplet()
//HERE I WANT TO GET tfNom.text and tfPrenom.text AND PUT THEM INTO A XML DOCUMENT IN A STRING
}

Answer

AEXML proved to me many times that it's very handy and easy to use. Use either Cocoapods, Carthage, or simply drag and drop the only file (AEXML.swift) into your project.

Your usage:

let yourXML = AEXMLDocument()
let attributes = ["xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance", "xmlns:xsd" : "http://www.w3.org/2001/XMLSchema"]
yourXML.addChild(name: "name", value: "Ben")
yourXML.addChild(name: "age", value: "18")
print(yourXML.xmlString)