简体   繁体   中英

Beautify xml in Python eTree

I have this simple py script that makes an xml file and saves, it and was wondering if there was a simple way to indent it?

import xml.etree.cElementTree as ET


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"

I looked at some other SO Q&A's Pretty printing XML in Python but these seem to mostly require other external libs? and was wondering if there is a way to not use those?

Thanks for the help.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM