I would like to parse an XML document using the Python module xml.etree.ElementTree. However, I want all the elements in the resulting object tree to have some class methods that I define. This involves creating my own subclass of the Python element class, but I had problems with the parser using its own subclass of the class when parsing, rather than in the built-in class.
For example, let's say I want the nodes in the tree to have a new custommethod () method. To do this, I subclass the element:
class MyElement(xml.etree.ElementTree._Element):
def custommethod():
. . .
Now that I am parsing a tree using
tree = xml.etree.ElementTree.parse(source)
I want all the elements in the tree to have a custommethod () method. So,
tree.getroot.custommethod()
should not fail.
, - ? Python .parse(), .