import xml.dom.minidom
dom = xml.dom.minidom.parse[xml_fname] # or xml.dom.minidom.parseString[xml_string]
pretty_xml_as_string = dom.toprettyxml[]
answered Jul 30, 2009 at 14:12
Ben NolandBen Noland
33.5k18 gold badges49 silver badges50 bronze badges
19
lxml is recent, updated, and includes a pretty print function
import lxml.etree as etree
x = etree.parse["filename"]
print etree.tostring[x, pretty_print=True]
Check out the lxml tutorial: //lxml.de/tutorial.html
Mad Physicist
101k24 gold badges164 silver badges248 bronze badges
answered Apr 15, 2009 at 0:21
17291729
4,8332 gold badges26 silver badges17 bronze badges
10
Another
solution is to borrow this indent
function, for use with the ElementTree library that's built in to Python since 2.5. Here's what that would look like:
from xml.etree import ElementTree
def indent[elem, level=0]:
i = "\n" + level*" "
j = "\n" + [level-1]*" "
if len[elem]:
if not elem.text or not elem.text.strip[]:
elem.text = i + " "
if not elem.tail or not elem.tail.strip[]:
elem.tail = i
for subelem in elem:
indent[subelem, level+1]
if not elem.tail or not elem.tail.strip[]:
elem.tail = j
else:
if level and [not elem.tail or not elem.tail.strip[]]:
elem.tail = j
return elem
root = ElementTree.parse['/tmp/xmlfile'].getroot[]
indent[root]
ElementTree.dump[root]
answered Jan 4, 2011 at 1:57
adeade
4,0361 gold badge19 silver badges25 bronze badges
7
You have a few options.
xml.etree.ElementTree.indent[]
Batteries included, simple to use, pretty output.
But requires Python 3.9+
import xml.etree.ElementTree as ET
element = ET.XML["text"]
ET.indent[element]
print[ET.tostring[element, encoding='unicode']]
BeautifulSoup.prettify[]
BeautifulSoup may be the simplest solution for Python < 3.9.
from bs4 import BeautifulSoup
bs = BeautifulSoup[open[xml_file], 'xml']
pretty_xml = bs.prettify[]
print[pretty_xml]
Output:
1 Add Visual Studio 2005 and 2008 solution files
This is my goto answer. The default arguments work as is. But text contents are spread out on separate lines as if they were nested elements.
lxml.etree.parse[]
Prettier output but with arguments.
from lxml import etree
x = etree.parse[FILE_NAME]
pretty_xml = etree.tostring[x, pretty_print=True, encoding=str]
Produces:
1 Add Visual Studio 2005 and 2008 solution files We need Visual Studio 2005/2008 project files for Windows.
This works for me with no issues.
xml.dom.minidom.parse[]
No external dependencies but post-processing.
import xml.dom.minidom as md
dom = md.parse[FILE_NAME]
# To parse string instead use: dom = md.parseString[xml_string]
pretty_xml = dom.toprettyxml[]
# remove the weird newline issue:
pretty_xml = os.linesep.join[[s for s in pretty_xml.splitlines[]
if s.strip[]]]
The output is the same as above, but it's more code.
answered Sep 14, 2016 at 4:54
ChaimGChaimG
6,2364 gold badges32 silver badges46 bronze badges
3
Here's my [hacky?] solution to get around the ugly text node problem.
uglyXml = doc.toprettyxml[indent=' ']
text_re = re.compile['>\n\s+[[^\s].*?]\n\s+\g