tags:

views:

220

answers:

3

Hi All,

I'm currently using the toprettyxml() function of the xml.dom module in a python script and I have some troubles with the newlines. If don't use the newl parameter or if I use toprettyxml(newl='\n') actually it displays several new lines instead of only one.

For instance

f = open(filename, 'w')
f.write(dom1.toprettyxml(encoding='UTF-8'))
f.close()

displayed:

<params>


    <param name="Level" value="#LEVEL#"/>


    <param name="Code" value="281"/>


</params>

Does anyone know where the problem comes from and how I can use it? FYI I'm using Python 2.6.1

A: 

Are you viewing the resulting file on Windows? If so, try using toprettyxml(newl='\r\n').

Will McCutchen
A: 

If you don't mind installing new packages, try beautifulsoup. I had very good experiences with its xml prettyfier.

felixhummel
+1  A: 

toprettyxml() is quiet awful. It is not a matter of Windows and '\r\n'. Trying any string as the newlparameter shows that two many lines are being added. Not only that, but other blanks (that may cause you problems when a machine reads the xml) are also added.

Some workarounds available at
http://ronrothman.com/public/leftbraned/xml-dom-minidom-toprettyxml-and-silly-whitespace

Xv
thanks a lot Xv ! Indeed, now, I'm trying to use toprettyxml() as few as possible but it's good to know there is a workaround for this annoying issue. And the post is very clear
PierrOz