This is a handy constructor that accomplishes the two basic operations in adding an element to a tree:
creating a new Element instance, and
adding that new Element as the next
child of its parent element.
Here is the general form:
SubElement ( parent, tag, attrib={}, **extras ):
The first argument, parent, is the Element instance under which the newly created
Element instance is to be added as its
next child.
The tag, attrib, and **extras arguments work exactly the same as they
do in the call to Element() described in
Section 6.2, “The Element() constructor”.
If you are working with multiple namespaces, you may also
include an nsmap keyword argument defining
your preferred namespace prefixes. For more on the nsmap argument, see Section 6.3, “The ElementTree() constructor”.
Here's an example. Suppose you want to build this XML:
<state name="New Mexico">
<county name="Socorro">
<ppl name="Luis Lopez"/>
</county>
</state>
Here's the code to build it, and then display it, interactively:
>>> st=etree.Element('state', name='New Mexico')
>>> co=etree.SubElement(st, 'county', name='Socorro')
>>> ppl=etree.SubElement(co, 'ppl', name='Luis Lopez')
>>> print etree.tostring(st)
<state name="New Mexico"><county name="Socorro"><ppl name="Luis Lopez"/>
</county></state>
>>>