[XML-SIG] new qp_xml.py

Laurent Szyster berini09@ibm.net
Thu, 11 Nov 1999 16:17:55 +0100


Walter,

Thanks for the comments.

Walter Doerwald wrote:
> 
> > Here are some suggestions (partly implemented in the attached
> > myXML.tar.gz archive):
> >
> >  . Split the building of the tree between a "Parser" class that
> >    instanciates the nodes and the "DOM" class that stores them
> >    (and eventually index them). This allow for more combination
> >    of features, hence better code re-use and extendability.
> 
> This parser class should have factory functions for every type
> of nodes it generates, which I can overwrite (something like
> createText(), createElement(), createComment(), etc.). This
> would e.g. make it possible to use different classes for the
> different element types. Or I could use extended node classes
> (probably via mixins) that provide additional functionality
> for all nodes.

Actually the objective of the design is to avoid subclassing
of the "Parser" class to implement features of the DOM or the
nodes. The parser is just there to spit out element nodes
consumed by the DOM.

> >  . Allow for direct mapping of XML to the Python namespace, in
> >    such way that you could refer to the second <p> element of
> >    the first <chapter> in a document as
> >
> >      dom.root.chapter[0].p[1]
> >
> >    That's very pythonic. It is implemented in a "DOM", and
> >    therefore can be combined with different "Parsers".
> 
> I've experimented with something similar to that. My element
> classes have a __getitem__ (and __setitem__ and __delitem__),
> when it's passed a string it returns (oder modifies or deletes)
> the attribute with this name, when it's passed a number i it
> returns the i'th child node. __getslice__ etc. or implemented
> for slices of child nodes to. I find this interface much more
> pythonic.

Well, I prefered not to use __getitem__, but rather directly
write to the __dict__ of the node. I suppose it's faster. Also,
this direct mapping to Python namespace is only one possible
behaviour implemented in the "DOM" class, not in the "Element"
class.

Have a look at the code (in parser.py), you'll see what I mean.

> I could post the code, if someone is interrested.

Sure I do ;-)


Laurent