4

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
}
10
  • Could you be more specific as to what you are trying to achieve? What have you tried? Are you aware that you can use NSDictionary.writeToFile() and NSArray.writeToFile to write the contents of a dictionary (alright, these are ObjC objects but there have to be Swift equivalents) or array to a Plist, which is in XML? Commented Jan 5, 2016 at 12:45
  • @Arc676 I have many variables, and I want to do a XML document with, thanks I'll do search on these Commented Jan 5, 2016 at 12:47
  • @EricD. no it's only for OS X Commented Jan 5, 2016 at 12:47
  • As far as I know, there is no "built-in" library on iOS which allows to write XML. Commented Jan 5, 2016 at 12:55
  • 1
    @Arc676 look edit please Commented Jan 5, 2016 at 14:28

1 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.