Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set attributes for an XML element with Python?

I am using ElementTree to build an XML file.

When I try to set an element's attribute with ET.SubElement().__setattr__(), I get the error AttributeError: __setattr__.

import xml.etree.cElementTree as ET
summary = open(Summary.xml, 'w')
root = ET.Element('Summary')
ET.SubElement(root, 'TextSummary')
ET.SubElement(root,'TextSummary').__setattr__('Status','Completed') # Error occurs here
tree = ET.ElementTree(root) 
tree.write(summary)
summary.close()

After code execution, my XML should resemble the following:

<Summary>
    <TextSummary Status = 'Completed'/>
</Summary>

How do I add attributes to an XML element with Python using xml.etree.cElementTree?

like image 954
Venkatesh Avatar asked Sep 13 '13 22:09

Venkatesh


People also ask

How can you declare attributes in XML?

An attribute should be declared using the attribute-list declaration in the DTD (Document Type Definition). An attribute element is used without any quotation and the attribute value is used in a single (' ') or double quotation (” “). An attribute name and its value should always appear in pair.

How do you access XML elements in Python?

To read an XML file using ElementTree, firstly, we import the ElementTree class found inside xml library, under the name ET (common convension). Then passed the filename of the xml file to the ElementTree. parse() method, to enable parsing of our xml file. Then got the root (parent tag) of our xml file using getroot().


1 Answers

You can specify attributes for an Element or SubElement during creation with keyword arguments.

import xml.etree.ElementTree as ET

root = ET.Element('Summary')
ET.SubElement(root, 'TextSummary', Status='Completed')

XML:

<Summary>
    <TextSummary Status="Completed"/>
</Summary>

Alternatively, you can use .set to add attributes to an existing element.

import xml.etree.ElementTree as ET

root = ET.Element('Summary')
sub = ET.SubElement(root, 'TextSummary')
sub.set('Status', 'Completed')

XML:

<Summary>
    <TextSummary Status="Completed"/>
</Summary>

Technical Explanation:

The constructors for Element and SubElement include **extra, which accepts attributes as keyword arguments.

xml.etree.ElementTree.Element(tag, attrib={}, **extra)
xml.etree.ElementTree.SubElement(parent, tag, attrib={}, **extra)

This allows you to add an arbitrary number of attributes.

root = ET.Element('Summary', Date='2018/07/02', Timestamp='11:44am')
# <Summary Date = "2018/07/02" Timestamp = "11:44am">

You can also use use .set to add attributes to a pre-existing element. However, this can only add one element at a time. (As suggested by Thomas Orozco).

root = ET.Element('Summary')
root.set('Date', '2018/07/02')
root.set('Timestamp', '11:44am')
# <Summary Date = "2018/07/02" Timestamp = "11:44am">

Full Example:

import xml.etree.ElementTree as ET

root = ET.Element('school', name='Willow Creek High')
ET.SubElement(root, 'student', name='Jane Doe', grade='9')
print(ET.tostring(root).decode())
# <school name="Willow Creek High"><student grade="9" name="Jane Doe" /></school>
like image 159
Stevoisiak Avatar answered Nov 04 '22 07:11

Stevoisiak