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.
f = open(filename, 'w') f.write(dom1.toprettyxml(encoding='UTF-8')) f.close()
<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
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
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()]) f.write(dom_string) f.close()
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?
toprettyxml(newl='') works for me on Windows.
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:
<parent> <child> Some text </child> </parent>
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
addindent='t' will turn out quite pretty.
TL;DR Indents and newlines remain from parsing and pretty print just adds more
If you don’t mind installing new packages, try beautifulsoup. I had very good experiences with its xml prettyfier.
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:
- Use dom.toprettyxml()
- Remove all white spaces
- Add new lines and tabs as per your requirement.
opening = 0
closing = 1
self_closing = 2
closing_tag = "</"
self_closing_tag = "/>"
opening_tag = "<"
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"" ?>