Next / Previous / Contents / TCC Help System / NM Tech homepage

8.3. Element.append(): Add a new element child

To add a new child c to an element E, use this method:

E.append ( c )

You can use this method to add Comment and ProcessingInstruction instances as children of an element, as well as Element instances.

Here is a conversational example:

>>> st = etree.Element ( "state", name="New Mexico" )
>>> etree.tostring(st)
'<state name="New Mexico"/>'
>>> co = etree.Element ( "county", name="Socorro" )
>>> st.append(co)
>>> etree.tostring(st)
'<state name="New Mexico"><county name="Socorro"/></state>'
>>> rem = etree.Comment ( "Just another day in paradise." )
>>> st.append ( rem )
>>> etree.tostring(st)
'<state name="New Mexico"><county name="Socorro"/><!-- Just another day in
paradise. --></state>'
>>>