Reputation: 16781
I'm writing an xml file with cElementTree like this:
cElementTree.ElementTree(xml_tree]).write(xmlPath, encoding="ISO-8859-1", xml_declaration=True)
This gives the following file (on Windows):
<?xml version='1.0' encoding='iso-8859-1'?><tag1 = "1"></tag1>
So the newlines are missing.
I tried adding the appropiate windows newline characters \r\n
'by hand', now I get this:
<?xml version='1.0' encoding='iso-8859-1'?><tag1 = "1">
</tag1>
However, I would like to have the correct newline character after each line, so that my output should look this:
<?xml version='1.0' encoding='iso-8859-1'?>
<tag1 = "1">
</tag1>
How can I achieve that?
Upvotes: 0
Views: 1930
Reputation:
lxml
supports pretty printing, cElementTree
doesn't.
from lxml import etree
xml_str = "<parent><child>text</child><child>other text</child></parent>"
root = etree.fromstring(xml_str)
print etree.tostring(root, pretty_print=True)
See Python pretty XML printer for XML string and Pretty printing XML in Python
Upvotes: 2