我正在学习 Swift 并希望根据用户输入创建一个 xml 文件。我能够创建文档根目录和一个子级别,但不能创建子级别。我不知道我错过了什么。我如何“漂亮地打印”到我的 xml?
所需的 xml 文件输出示例:
<find_sites>
<name>Many Sites</name>
<sites>8</sites>
<site_info>
<site_name>First Site</site_name>
<site_description>Description of first site</site_description>
</site_info>
</find_sites>
我在操场上的 Swift 代码:
let root = NSXMLElement(name: "find_sites")
let xmlFile = NSXMLDocument (rootElement: root)
root.addChild(NSXMLElement(name: "name", stringValue: "Many Sites"))
root.addChild(NSXMLElement(name: "sites", stringValue: "8"))
let site_info = NSXMLElement(name: "site_info")
root.addChild(NSXMLElement(name: "site_info"))
site_info.addChild(NSXMLElement(name: "site_name", stringValue: "First Site"))
site_info.addChild(NSXMLElement(name: "site_description", stringValue: "Description"))
println(" \(xmlFile.XMLString)")
Results: <find_sites><name>Many Sites</name><sites>8</sites><site_info></site_info></find_sites>
我的“site_info”子元素被忽略。