By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,758 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

Understanding the arguments for SubElement factory in ElementTree

P: n/a
Can the **extra argument in the SubElement() factory in ElementTree be
used to set the text property? Example:

Want the text associated with the <subroot> tag to be xyz.

<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')

Oct 29 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Your SubElement call is lacking the attrib argument, but you can't set
text, anyway.

The elementtree source makes it clear, you can only set element attrib
attributes
with SubElement

def SubElement(parent, tag, attrib={}, **extra):
attrib = attrib.copy()
attrib.update(extra)
element = parent.makeelement(tag, attrib)
parent.append(element)
return element

Oct 29 '05 #2

P: n/a
mi************@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
Oct 30 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.