You could use the standard library's minidom module's toprettyxml
method:
import xml.dom.minidom as minidom
xml = minidom.Document()
root = xml.createElement("root")
xml.appendChild(root)
doc = xml.createElement("doc")
doc.setAttribute("location", "one")
root.appendChild(doc)
field = xml.createElement("field1")
field.setAttribute("name", "blah")
text = xml.createTextNode("some value1")
field.appendChild(text)
doc.appendChild(field)
field = xml.createElement("field2")
field.setAttribute("name", "asdfasd")
text = xml.createTextNode("some value2")
field.appendChild(text)
doc.appendChild(field)
print(xml.toprettyxml(indent=' '*4))
yields
<?xml version="1.0" ?>
<root>
<doc location="one">
<field1 name="blah">some value1</field1>
<field2 name="asdfasd">some value2</field2>
</doc>
</root>
Or, if you prefer the ElementTree
methods for creating XML and don't mind
being a bit inefficient, you could use ElementTree
to write the unformatted XML
to a StringIO (for Python2) or ByteIO (for Python3), parse that into a minidom
Document, and then write it back out again using toprettyxml
:
import xml.etree.cElementTree as ET
import xml.dom.minidom as minidom
try:
# for Python2
from cStringIO import StringIO as BytesIO
except ImportError:
# for Python3
from io import BytesIO
root = ET.Element("root")
doc = ET.SubElement(root, "doc", location="one")
ET.SubElement(doc, "field1", name="blah").text = "some value1"
ET.SubElement(doc, "field2", name="asdfasd").text = "some vlaue2"
buf = BytesIO()
buf.write(ET.tostring(root))
buf.seek(0)
root = minidom.parse(buf)
print(root.toprettyxml(indent=' '*4))