XML minidom Parsing and ToPrettyXML

Paul Kozik zykhou at gmail.com
Sun Mar 25 19:57:52 EDT 2007


I am trying to write a script that reads an XML file (using the
minidom module), makes an edit to a few attributes, then saves it
back. If I use minidom.Document() to create the xml file, then write
it with toprettyprint, it looks fine.

However, if I use xml.minidom.parse to parse the xml document, change
a few attributes with setAttribute, then write back with toprettyxml,
my XML file gets loaded up with spaces between many of the elements.

Like this,
<?xml ... ?>
<root>

       <tile />



       <tile />



       <tile />
</root>

I assume this has to do with the way xml.dom.minidom.parse parses the
file, because it will even do this without changing any attributes.
This whitespace damages readability, and bloats up the file.

Anyone know of a way to make the file look as it should, hopefully in
a pythonic way?



More information about the Python-list mailing list