Created
July 11, 2017 08:16
-
-
Save greggjaskiewicz/7b71387fec0fb90a2df8571320e69ef2 to your computer and use it in GitHub Desktop.
creating simple xml document using NSXML in swift 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
let node1 = XMLElement(name: "first", stringValue: "Donnie") | |
let node2 = XMLElement(name: "last", stringValue: "Duck") | |
let nameNode = XMLElement(name: "name") | |
nameNode.addChild(node1) | |
nameNode.addChild(node2) | |
let root = XMLElement(name: "names") | |
root.addChild(nameNode) | |
let document = XMLDocument(rootElement: root) | |
let xmlData = document.xmlData(options: .nodePrettyPrint) | |
// swift 3 version: | |
//let xmlData = document.xmlData(withOptions: Int(XMLNode.Options.nodePrettyPrint.rawValue) ) | |
try? xmlData.write(to: URL(fileURLWithPath: "/tmp/test.xml")) | |
/* | |
cat /tmp/test.xml | |
<names> | |
<name> | |
<first>Donnie</first> | |
<last>Duck</last> | |
</name> | |
</names> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment