简体   繁体   English

在iOS上用swift编写XML

[英]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? 我正在尝试找到一个创建XML文档的解决方案并将其放在一个String中,我做了一些关于如何在swift中创建生成或编写xml的研究,但我发现只有库而且我不想使用它们,有解决方案吗?

I have some UITextFields , the user put values, when they click on a button, I want to create a xml document. 我有一些UITextFields ,用户输入值,当他们点击按钮时,我想创建一个xml文档。

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
}

AEXML proved to me many times that it's very handy and easy to use. AEXML多次向我证明它非常方便易用。 Use either Cocoapods, Carthage, or simply drag and drop the only file (AEXML.swift) into your project. 使用Cocoapods,Carthage或简单地将唯一文件(AEXML.swift)拖放到项目中。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM