Skip to content

Instantly share code, notes, and snippets.

@greggjaskiewicz
Created July 11, 2017 08:16
Show Gist options
  • Save greggjaskiewicz/7b71387fec0fb90a2df8571320e69ef2 to your computer and use it in GitHub Desktop.
Save greggjaskiewicz/7b71387fec0fb90a2df8571320e69ef2 to your computer and use it in GitHub Desktop.
creating simple xml document using NSXML in swift 4
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