Python Return Xmlblock And Remove Encoding Tag
I am trying to edit an xml file. The file i get, where the new information is stored, looks like this:
Solution 1:
Pretty print an element only with lxml
>>> from lxml import etree
>>> e1 = etree.Element("root")
>>> e2 = etree.Element("child")
>>> e2.text = "algo"
>>> e1.append(e2)
>>> etree.tostring(e1,pretty_print=True)
b'<root>\n <child>algo</child>\n</root>\n'
>>> etree.tostring(e1,pretty_print=True).decode('utf-8')
'<root>\n <child>algo</child>\n</root>\n'
>>> print(etree.tostring(e1,pretty_print=True))
<root>
<child>algo</child>
</root>
No XML declaration on top, just the element's content.
This creates a XML representation of the string that when printed will include the XML declaration
minidom.parseString(rough_string)
Solution 2:
@LMC
i managed to implement your suggestion, and it is giving me the output i need. But when i print it all, i only get it with the characters '\n' and not in "pretty".
I modified the function like this, i hope i interpreted your answer right.
def prettify(elem):
pretty = ET.tostring(elem,pretty_print=True)
return pretty
Post a Comment for "Python Return Xmlblock And Remove Encoding Tag"