Problem round-tripping with xml.dom.minidom pretty-printer

Ben Butler-Cole ben.butlercole at
Fri Feb 29 17:12:47 CET 2008


I have run into a problem using minidom. I have an HTML file that I
want to make occasional, automated changes to (adding new links). My
strategy is to parse it with minidom, add a node, pretty print it and
write it back to disk.

However I find that every time I do a round trip minidom's pretty
printer puts extra blank lines around every element, so my file grows
without limit. I have found that normalizing the document doesn't make
any difference. Obviously I can fix the problem by doing without the
pretty-printing, but I don't really like producing non-human readable

Here is some code that shows the behaviour:

    import xml.dom.minidom as dom
    def p(t):
        d = dom.parseString(t)
        t2 = d.toprettyxml()
        print t2

Does anyone know how to fix this behaviour? If not, can anyone
recommend an alternative XML tool for simple tasks like this?


More information about the Python-list mailing list