How can I remove the whitespaces and line breaks in an XML string in Python 2.6? I tried the following packages:
etree: This snippet keeps the original whitespaces:<
The only thing that bothers me about xml.dom.minidom's toprettyxml() is that it adds blank lines. I don't seem to get the split components, so I just wrote a simple function to remove the blank lines:
#!/usr/bin/env python
import xml.dom.minidom
# toprettyxml() without the blank lines
def prettyPrint(x):
for line in x.toprettyxml().split('\n'):
if not line.strip() == '':
print line
xml_string = "\nsomething \nparrot \n "
# parse XML
x = xml.dom.minidom.parseString(xml_string)
# clean
prettyPrint(x)
And this is what the code outputs:
something
parrot
If I use toprettyxml() by itself, i.e. print(toprettyxml(x)), it adds unnecessary blank lines:
something
parrot