I have an xml file and I used Elementtree to add a new tag to the xml file. My XML file before processing looks like this
<?xml version="1.0" encoding="utf-8"?> <PackageInfo xmlns="http://someurlpackage"> <data ID="http://someurldata1">data1</data > <data ID="http://someurldata2">data2</data > <data ID="http://someurldata3">data3</data > </PackageInfo>
I used the following python code to add a new data tag and write it to my XML file
tree = ET.ElementTree(xmlFile) root = tree.getroot() elem= ET.Element('data') elem.attrib['ID']="http://someurldata4" elem.text='data4' root[1].append(elem) tree = ET.ElementTree(root) tree.write(xmlFile)
But the resulting XML file has <?xml version="1.0" encoding="utf-8"?> missing and the file looks below
<PackageInfo xmlns="http://someurlpackage"> <data ID="http://someurldata1">data1</data > <data ID="http://someurldata2">data2</data > <data ID="http://someurldata3">data3</data > </PackageInfo>
Is there a way to include the xml header instead of hard-coding the string
source share