[XML-SIG] pDomlette and importNode

Alan Kernnedy pyxml@xhaus.com
Mon, 24 Sep 2001 08:26:10 +0100


Alexandre,

Thanks for the elegant and Pythonic solution to my "importNode" problem. I must
learn to adapt the Pythonic style more myself, i.e. extend a class through
manipulation of its definition rather than going and hacking the original source
file.

I've now got my little app running with the pDomlettte, and the change is fantastic.
The memory footprint is hugely smaller, and the speed has increased by about 8 to 10
times.

Many thanks,

Alan.

Alexandre Fayolle wrote:

> On Fri, 21 Sep 2001, Xhaus Main Account wrote:
>
> > Thanks for the tips Alexander.
> >
> > However, from what I can see, pDomlette and cDomlette are not suitable for my
> > needs due to incomplete DOM support (I'll briefly explain my app below).
> >
> > However, I do need support for "importNode". I can see that pDomlette does not
> > have it, and I think cDomlette doesn't have it either (I checked by doing
> > "strings cDomlettec.pyd | grep importNode". Am I wrong here?)
>
> cDomlette is for now readonly, so it won't support importNode.  pDomlette
> does not. We needed this in Narval, as well as removeAttributeNS, so what
> we do is add the methods dynamically to the class at start up. Now, I'm
> pretty sure that if this is asked to the Fourthought people, they could
> consider adding the method to pDomlette in the next release (and to
> cDomlette which, I am told, should be read-write in the near future).
>
> In the meantime, here's a snippet which adds importNode and
> removeAttributeNS to pDomlette nodes:
>
> -----------------8<-----------------------------
> from Ft.Lib.pDomlette import Element,Document
>
> def myremoveAttributeNS(self,nsURI,lname):
>     node = self.getAttributeNodeNS(nsURI,lname)
>     if node:
>         self.removeAttributeNode(node)
>     return
>
> Element.removeAttributeNS = myremoveAttributeNS
>
> def _changeowner(node,owner):
>     node.ownerDocument = owner
>     for a in node.attributes or []:
>         a.ownerDocument = owner
>     [_changeowner(c,owner) for c in node.childNodes or []]
>
> def myimportNode(self,importedNode,deep=0):
>     try:
>         new_node = importedNode.cloneNode(deep)
>         _changeowner(new_node,self)
>         return new_node
>     except Exception,e:
>         print importedNode.__class__
>         raise e
>
> Document.importNode = myimportNode
> ---------------------8<---------------------------
>
> Alexandre Fayolle
> --
> LOGILAB, Paris (France).
> http://www.logilab.com   http://www.logilab.fr  http://www.logilab.org
> Narval, the first software agent available as free software (GPL).