Given :
<message><warning>Louis was here</warning></message>
You can use :
import lxml.etree as etree # deb package : python-lxml fic='test.xml' x = etree.parse(fic) with open(fic+'.pretty','w') as f: f.write(etree.tostring(x, pretty_print = True))
Output :
<message> <warning>Louis was here</warning> </message>
Many parsers don’t like extra spaces surrounding the content of text nodes
Given :
<message> <warning> Louis was here </warning> </message>
You can use :
import re
def fixxml(xmlstr):
fix = re.compile(r'((?<=>)(\n[ ]*)(?=[^< ]))|(?<=[^> ])(\n[ ]*)(?=<)')
return re.sub(fix, '', xmlstr.expandtabs())
fic='test.xml'
with open(fic) as f: print fixxml(f.read())
Output :
<message> <warning>Louis was here</warning> </message>