Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML header getting 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 is as follows

<?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 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 resultant xml file have <?xml version="1.0" encoding="utf-8"?> absent and the file looks as 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 any way to include the xml header rather than hardcoding the line

like image 797
mystack Avatar asked Sep 17 '12 10:09

mystack


People also ask

What is ElementTree?

The cElementTree module is a C implementation of the ElementTree API, optimized for fast parsing and low memory use. On typical documents, cElementTree is 15-20 times faster than the Python version of ElementTree, and uses 2-5 times less memory.

What does Etree parse do?

Parsing from strings and files. lxml. etree supports parsing XML in a number of ways and from all important sources, namely strings, files, URLs (http/ftp) and file-like objects. The main parse functions are fromstring() and parse(), both called with the source as first argument.


2 Answers

It looks like you need optional arguments to 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'm afraid I'm not that familiar with xml.etree.ElementTree and it's variation between python releases.

Here's it working 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>'
like image 100
MattH Avatar answered Sep 24 '22 01:09

MattH


try this:::

tree.write(xmlFile, encoding="utf-8")
like image 36
namit Avatar answered Sep 24 '22 01:09

namit