Problem with newlines when I use toprettyxml()

Posted on

Question :

Problem with newlines when I use toprettyxml()

I’m currently using the toprettyxml() function of the xml.dom module in a Python script and I’m having some trouble with the newlines.
If don’t use the newl parameter or if I use toprettyxml(newl='n') it displays several newlines instead of only one.

For instance

f = open(filename, 'w')



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

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


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

Asked By: PierrOz


Answer #1:

toprettyxml() is quite awful. It is not a matter of Windows and ‘rn’. Trying any string as the newlparameter shows that too 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

Answered By: xverges

Answer #2:

I found another great solution :

f = open(filename, 'w')
dom_string = dom1.toprettyxml(encoding='UTF-8')
dom_string = os.linesep.join([s for s in dom_string.splitlines() if s.strip()])

Above solution basically removes the unwanted newlines from the dom_string which are generated by toprettyxml().

Inputs taken from -> What’s a quick one-liner to remove empty lines from a python string?

Answered By: dganesh2002

Answer #3:

toprettyxml(newl='') works for me on Windows.

Answered By: OndrejC

Answer #4:

This is a pretty old question but I guess I know what the problem is:

Minidoms pretty print has a pretty straight forward method. It just adds the characters that you specified as arguments. That means, it will duplicate the characters if they already exist.

E.g. if you parse an XML file that looks like this:

      Some text

there are already newline characters and indentions within the dom. Those are taken as text nodes by minidom and are still there when you parse it it into a dom object.

If you now proceed to convert the dom object into an XML string, those text nodes will still be there. Meaning new line characters and indent tabs are still remaining. Using pretty print now, will just add more new lines and more tabs. That’s why in this case not using pretty print at all or specifying newl='' will result in the wanted output.

However, you generate the dom in your script, the text nodes will not be there, therefore pretty printing with newl='rn' and/or addindent='t' will turn out quite pretty.

TL;DR Indents and newlines remain from parsing and pretty print just adds more

Answered By: Link64

Answer #5:

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

Answered By: felixhummel

Answer #6:

Following function worked for my problem.
I had to use python 2.7 and i was not allowed to install any 3rd party additional package.

The crux of implementation is as follows:

  1. Use dom.toprettyxml()
  2. Remove all white spaces
  3. Add new lines and tabs as per your requirement.


import os
import re
import xml.dom.minidom
import sys

class XmlTag:
opening = 0
closing = 1
self_closing = 2
closing_tag = "</"
self_closing_tag = "/>"
opening_tag = "<"

def to_pretty_xml(xml_file_path):
pretty_xml = ""
space_or_tab_count = " " # Add spaces or use t
tab_count = 0
last_tag = -1

dom = xml.dom.minidom.parse(xml_file_path)

# get pretty-printed version of input file
string_xml = dom.toprettyxml(' ', os.linesep)

# remove version tag
string_xml = string_xml.replace("<?xml version="1.0"" ?>
, '')

Leave a Reply

Your email address will not be published.