1

I'm working on a project to store various bits of text in xml files, but because people besides me are going to look at it and use it, it has to be properly indented and such. I looked at a question on how to generate xml files using cElement Tree here, and the guy says something about putting in info about making things pretty if people ask, but there isn't anything there (I guess because no one asked). So basically, is there a way to properly indent and whitespace using cElementTree, or should i just throw up my hands and go learn how to use lxml.

Community
  • 1
  • 1
Cease
  • 1,082
  • 1
  • 10
  • 20
  • Possible duplicate http://stackoverflow.com/questions/749796/pretty-printing-xml-in-python – Stack of Pancakes Jul 17 '14 at 21:33
  • That says how to use prettyprint with xml yes, but I wanted to see if there was an option with ElementTree before diving into another module – Cease Jul 17 '14 at 21:35
  • 1
    ElementTree makes no effort to “pretty print” the output produced by tostring(), since adding extra whitespace changes the contents of the document. To make the output easier to follow for human readers you'll either need to write a custom function to do the formatting, or use `import xml.dom.minidom`. The xml module is part of core python, no reason not to use it. – Stack of Pancakes Jul 17 '14 at 21:41
  • 2
    @CeaseofMorality Consider using `lxml`, it has good set of additional features and is worth the effort to install it. E.g. Google application engine does not accept any python package, which is not pure python, with very few exceptions likey pyyaml -- and lxml. I guess, they do this for good reason. – Jan Vlcinsky Jul 17 '14 at 22:09

2 Answers2

1

You can use minidom to prettify our xml string:

from xml.etree import ElementTree as ET
from xml.dom import minidom

# Return a pretty-printed XML string for the Element.
def prettify(xmlStr):
    INDENT = "    "
    rough_string = ET.tostring(xmlStr, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent=INDENT)

# name of root tag
root = ET.Element("root")

child = ET.SubElement(root, 'child')
child.text = 'This is text of child'

prettified_xmlStr = prettify(root)
output_file = open("Output.xml", "w")
output_file.write(prettified_xmlStr)
output_file.close()
print("Done!")
Blue Day
  • 21
  • 2
0

Answering myself here:

Not with ElementTree. The best option would be to download and install the module for lxml, then simply enable the option

prettyprint = True

when generating new XML files.

Cease
  • 1,082
  • 1
  • 10
  • 20