[XML-SIG] new qp_xml.py
Walter Doerwald
walter@data.franken.de
Wed, 10 Nov 1999 11:00:42 +0200
On Tue, 09 Nov 1999 14:50:08 +0100 Laurent Szyster wrote:
> Greg Stein wrote:
> >
> > Suggestions/changes/etc are welcome! (Laurent... :-)
>
> 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.
> Here the important is the definition of the interface
> between the "Parser" and the "DOM": the "Element" class
> and the "DOM.append()" method.
>
> . 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.
I could post the code, if someone is interrested.
> [...]
Servus...
Walter
--
Walter Dörwald · walter@data.franken.de · Kommunikationnetz Franken e.V.