[XML-SIG] Wrapping a DOM class.
Fred L. Drake, Jr.
fdrake@acm.org
Wed, 27 Jun 2001 14:33:13 -0400 (EDT)
Ian Sparks writes:
> I confess the XML support for Python confuses me. I thought I understood the
> DOM but I'm having problems with the Python DOM implementations. The
> following sample shows what I am *trying* to do. I know the minidom won't
> get me here so what implementation will? If someone could convert to a
> working sample I'd be very grateful.
minidom will work just fine; the problem you're having has nothing
to do with the DOM.
What's happening is that you are defining an attribute for xdoc
instances named "dom"; that's a class object, and has no
documentElement attribute itself. You can get rid of the "dom =
Document" assignment in the class definition.
Once that's out of the way, the constructor will attempt to create
the "dom" attribute, but that will go through the __setattr__() hook,
which then expects to find the not-yet-created "dom" attribute, so the
__getattr__() is called with the name "dom". That then uses the
not-yet-created "dom" attribute to get the document object, causing a
recursive call to __getattr__() -- this will eventually fail.
If you change the constructor to avoid the __setattr__(), the rest
of the class needs no change:
------------------------------------------------------------------------
class xdoc:
def __init__(self):
self.__dict__['dom'] = parseString('<myxml att="hello"/>')
def __getattr__(self,key):
return self.dom.documentElement.getAttributeNS('',key)
def __setattr__(self,key,value):
self.dom.documentElement.setAttributeNS('',key,value)
------------------------------------------------------------------------
And it works now, to boot!
-Fred
--
Fred L. Drake, Jr. <fdrake at acm.org>
PythonLabs at Digital Creations