Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/xml_write.py
Created January 7, 2014 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/8300729 to your computer and use it in GitHub Desktop.
Save zeffii/8300729 to your computer and use it in GitHub Desktop.
from xml.sax.saxutils import XMLGenerator
import StringIO
import xml.dom.minidom
# unformatted
with open("dragons.xml", "w") as outfile:
w = XMLGenerator(outfile, 'utf-8')
w.startDocument()
w.startElement("test", {'bar': 'baz'})
w.characters("Foo")
w.endElement("test")
w.endDocument()
# formatted
with open("dragons.xml") as infile:
w = infile.read()
dom = xml.dom.minidom.parseString(w)
k = dom.toprettyxml()
print(k)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment