'<?xml version="1.0" encoding="utf-8"?>' disappears after encoding xml to string python

2.5k Views Asked by At

In my xml file I have <?xml version="1.0" encoding="utf-8"?> at the beginning. But it disappears if I encode it to a string. By that I mean my string does not have it anymore at the beginning. I thought I can simply insert it in my string like in the code below (which worked when printing it), but when I wanted to save the string as a xml again on my laptop and open it, <?xml version="1.0" encoding="utf-8"?> wasn't there anymore.

import xml.etree.ElementTree as ET

tree = ET.parse(r'someData.xml')
root = tree.getroot()

xml_str = ET.tostring(root, encoding='unicode')
xml_str = '<?xml version="1.0" encoding="utf-8"?>' + xml_str

Does anybody know how to encode the xml to a string without loosing <?xml version="1.0" encoding="utf-8"?> OR how to save the string as xml without loosing it? My aim is to have it in my exported xml. Thank you in advance!!

1

There are 1 best solutions below

0
On

To make it more visible:

Before I saved the file so:

tree = ET.ElementTree(ET.fromstring(xml_str)) 
tree.write(open('test2.xml', 'a'), encoding='unicode')

But now, I save it like this so I don't miss the declaration at the beginning of the xml file:

tree = ET.ElementTree(ET.fromstring(xml_str)) 
tree.write(open('test.xml', 'wb'), encoding="utf-8", xml_declaration=True)