pythonxmlxml-parsingelementtree

Pyrhon parse XML drop XML header


I have the following code to edit XML

import xml.etree.ElementTree as ET


data = b'<?xml version="1.0" encoding="UTF-8"?><aaaa version="1.0"><zzz>xxxxxx</zzz><bbbb>11111</bbbb></aaaa>'

root = ET.fromstring(data)

# Find the element to update
zzz_element = root.find('zzz')
if zzz_element is not None:
    # Update the element's text
    zzz_element.text = 'new_value'

# Convert the updated XML back to a string
updated_data = ET.tostring(root, encoding='utf-8', method='xml').decode()

print(updated_data)

The output of this program is:

<aaaa version="1.0"><zzz>new_value</zzz><bbbb>11111</bbbb></aaaa>

Why does the header of XML:

<?xml version="1.0" encoding="UTF-8"?>

Drop out? Can I edit the xml, and parse it to string with keep the header


Solution

  • You need to inform ElementTree.tostring that you want header by setting xml_declaration to True, consider following simple example

    import xml.etree.ElementTree as ET
    data = b'<?xml version="1.0" encoding="UTF-8"?><thing>abc</thing>'
    root = ET.fromstring(data)
    root.set("somekey", "somevalue")
    updated_data = ET.tostring(root, encoding='utf-8', method='xml', xml_declaration=True).decode()
    print(updated_data)
    

    gives output

    <?xml version='1.0' encoding='utf-8'?>
    <thing somekey="somevalue">abc</thing>