[issue21028] ElementTree objects should support all the same methods are Element objects
Raymond Hettinger
report at bugs.python.org
Sat Mar 22 23:47:56 CET 2014
New submission from Raymond Hettinger:
The inner objects are Elements which has a great deal of flexiblity (for example, they can be iterated over directly). The outermost object is an ElementTree which lacks those capabilities (it only supports findall).
For example in a catalog of books:
catalog = xml.etree.ElementTree.parse('books.xml')
# This succeeds
for book in catalog.findall('book'):
print(book.tag)
# This fails:
for book in catalog:
print(book.tag)
# But for inner elements, we have more options
book = catalog.find('bk101')
for subelement in book:
print(subelement.tag)
Here are the differences between the API for ElementTree and Element
In [9]: set(dir(book)) - set(dir(catalog))
Out[9]:
{'__delitem__',
'__getitem__',
'__len__',
'__nonzero__',
'__setitem__',
'_children',
'append',
'attrib',
'clear',
'copy',
'extend',
'get',
'getchildren',
'insert',
'items',
'itertext',
'keys',
'makeelement',
'remove',
'set',
'tag',
'tail',
'text'}
In [10]: set(dir(catalog)) - set(dir(book))
Out[10]: {'_root', '_setroot', 'getroot', 'parse', 'write', 'write_c14n'}
Note, the XML data model requires that the outermost element have some capabilities that inner elements don't have (such as comments and processing instructions). That said, the outer element shouldn't have fewer capabilities that the inner elements.
----------
components: Library (Lib)
messages: 214521
nosy: rhettinger
priority: normal
severity: normal
status: open
title: ElementTree objects should support all the same methods are Element objects
type: enhancement
versions: Python 3.5
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21028>
_______________________________________
More information about the Python-bugs-list
mailing list