XML header removed after processing with elementtree

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

+4
source share
3 answers

It looks like you need additional arguments for the write method to output the declaration.

http://docs.python.org/library/xml.etree.elementtree.html#elementtree-elementtree-objects

 tree.write(xmlfile,xml_declaration=True) 

I am afraid that I am not familiar with xml.etree.ElementTree and this is a variation between python releases.

Here it works with lxml.etree :

 >>> from lxml import etree >>> sample = """<?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>""" >>> >>> doc = etree.XML(sample) >>> data = doc.makeelement("data") >>> data.attrib['ID'] = 'http://someurldata4' >>> data.text = 'data4' >>> doc.append(data) >>> etree.tostring(doc,xml_declaration=True) '<?xml version=\'1.0\' encoding=\'ASCII\'?>\n<PackageInfo xmlns="http://someurlpackage">\n<data ID="http://someurldata1">data1</data>\n<data ID="http://someurldata2">data2</data>\n<data ID="http://someurldata3">data3</data>\n<data ID="http://someurldata4">data4</data></PackageInfo>' >>> etree.tostring(doc,xml_declaration=True,encoding='utf-8') '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<PackageInfo xmlns="http://someurlpackage">\n<data ID="http://someurldata1">data1</data>\n<data ID="http://someurldata2">data2</data>\n<data ID="http://someurldata3">data3</data>\n<data ID="http://someurldata4">data4</data></PackageInfo>' 
+8
source

try it:

 tree.write(xmlFile, encoding="utf-8") 
+3
source

If you are using python <= 2.6
In ElementTree.write ()

no xml_declaration parameter
 def write(self, file, encoding="us-ascii"): def _write(self, file,node, encoding, namespaces): 

You can use lxml.etree
install lxml
sample here:

 from lxml import etree document = etree.Element('outer') node = etree.SubElement(document, 'inner') print(etree.tostring(document, xml_declaration=True)) 

BTW:
I believe there is no need to write xml_declaration. Is an XML node declaration mandatory?

The document does not require an XML declaration to be read successfully, since there are default values ​​for both versions and coding (1.0 and UTF-8, respectively).

At least it works even if AndroidManifest.xml does not have xml_declaration
I tried: -)

0
source

Source: https://habr.com/ru/post/1434580/


All Articles