Understanding the arguments for SubElement factory in ElementTree

Giovanni Bajo noway at sorry.com
Sun Oct 30 14:12:03 EST 2005


mirandacascade at yahoo.com wrote:

> <root>
>   <subroot>xyz</subroot>
> </root>
>
> rather than:
> root = Element('root')
> subroot = SubElement(root, 'subroot')
> subroot.text = 'xyz'
>
> Was wondering whether this code accomplish that
> root = Element('root')
> subroot = SubElement(root, 'subroot', text='xyz')


No, this creates:

<root>
  <subroot text="xyz" />
</root>

I believe the text ought to be set in a separate statement.

Giovanni Bajo





More information about the Python-list mailing list