6

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

3
  • 3
    The prolog (<?xml ... ?>) is optional, the values in it the default. Are you sure you need it? Commented Sep 17, 2012 at 10:29
  • yes i need it Is there any other way rather than hardfixing Thanks Commented Sep 17, 2012 at 10:34
  • @user1654136 the other option is to update to a more recent Python. Commented Sep 17, 2012 at 13:59

3 Answers 3

12

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>'
Sign up to request clarification or add additional context in comments.

4 Comments

iam getting following error but iam using python 2.6 "write() got an unexpected keyword argument 'xml_declaration'"
@user1654136: what library are you using?
The Python 2.6 docs don't include the xml_declaration argument: it was presumably added later. So I think the full answer is that with the 2.6 version of ElementTree you only get the declaration when it is needed, you can't force it.
@user1654136: If you are on python 2.6, use lxml as a (faster) alternative to the elementtree module. Same API, including the xml_declaration option, as the ElementTree module.
5

try this:::

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

1 Comment

Tried it on Red Hat Linux release 7.0 (Guinness) along w/ Python2.5 it doesn't embed the XML deceleration ...
0

If you are using python <=2.6
There is no xml_declaration parameter in ElementTree.write()

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 find that it is not necessary to write the xml_declaration
Is the XML declaration node mandatory?

There is no XML declaration necessary for a document to be successfully readable, since there are defaults for both version and encoding (1.0 and UTF-8, respectively).

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.