[lxml-dev] Custom Elements question
Stefan Behnel
stefan_ml at behnel.de
Fri May 2 08:49:39 CEST 2008
Alex Klizhentas wrote:
>> Alex Klizhentas wrote:
>>> I've extended the ElementBase object using the approach described in the
>>> tutorial, but SubElement does not work as desired:
>>>
>>> class NodeBase(etree.ElementBase):
>>> def append(self,child):
>>> print "aaa"
>>> return etree.ElementBase.append(self,child)
>>>
>>> etree.SubElement(root,"child") #no "aaa" printed
>> That's because SubElement() does not call .append().
>>
>>
>>> OK, but when taking your code to the module:
>>>
>>> def SubElement(parent, tag, attrib={}, **extra):
>>> attrib = attrib.copy()
>>> attrib.update(extra)
>>> element = parent.makeelement(tag, attrib)
>>> parent.append(element)
>>> return element
>
> The idea behind this is to allow the XML tree to notify observers when it's
> contents are changed: the node is added, removed or moved.
>
> That's why I'm going to override the ElementBase members so that they will
> notify observers on the certain actions performed.
>
> Everything works fine, except this usefult SubElement function that did not
> work as expected, now you've clarified the things,
Ah, sure. Then it's best to use a pure Python implementation of SubElement
instead, as the one above.
Stefan
More information about the lxml-dev
mailing list