[lxml-dev] Custom Elements question
Alex Klizhentas
klizhentas at gmail.com
Thu May 1 21:11:38 CEST 2008
Thanks for the comments,
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,
Thanks
Alex
2008/5/1 Stefan Behnel <stefan_ml at behnel.de>:
> Hi,
>
> 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
> >
> > SubElement(root,"child") # "aaa" is here!
>
> As expected, as you call .append() explicitly here.
>
>
> > and overriding
> > def makeelement(self, tag, attrib):
> > return Node(tag, attrib)
> >
> > in the NodeBase just does not help,
>
> SubElement() does not call .makeelement() either. It's implemented in
> plain C.
> Could you explain a bit why you want to do this and how your .append()
> differs
> from the normal append code?
>
> Stefan
>
--
Regards,
Alex
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://codespeak.net/pipermail/lxml-dev/attachments/20080501/0c565b12/attachment.htm
More information about the lxml-dev
mailing list