[XML-SIG] Simple DOM API a la dom4j/jdom

Gerhard Häring haering_python@gmx.de
Wed, 17 Jul 2002 05:33:28 +0200


* Matt G. <matt_g_@hotmail.com> [2002-07-17 03:03 +0000]:
> Gerhard wrote:
> >Does such a thing exist with a BSD-style license somewhere for Python?
> >Now that I try to use XML again after a long time, I find the DOM API
> >just too clumsy, and have written a very basic DOM wrapper that allows
> >me to do things like:
> >
> >doc = reader.fromStream(open("flow.xml"))
> >print DOMWrapper(doc)["startnode"][0].name
> >      ^              ^            ^   ^
> >      Wrapper        Element coll.|   attribute
> >                                  |
> >                                  First element, also supports iterators
> 
> It seems that you want either XPath (http://www.w3c.org/TR/xpath) or
> XML Query (http://www.w3c.org/XML/Query). [...] 4Suite includes an
> XPath implementation,

It's apparently included PyXML 0.7.1, but not documented at all, AFAIC.

> and uses the Apache license (I don't know if that's similar enough to
> the BSD license, for you).

Would be fine.

> [...] I poked around at the API documentation, and didn't find such a
> high-level interface as you seem to want. Maybe I just missed it. If
> not, I think it's a shame, since I wanted to use XPath w/ DOM, too.

I once did use the DOM API in Java, and after playing around with the
various alternatives (pure DOM, simplified DOM API, XML binding), I went
with dom4j, which has an interface like I described in my example above.

I really think that such a module should be commonly available for
Python, as it's sort of grotesque that using Java (with dom4j/jdom) for
accessing DOM trees is currently easier than using Python ;-)

Btw. my Python DOM wrapper seems to work ok, and it's only something
like 40 lines long. No test suite, namespaces or other fancy stuff yet.

Now this leads me to another question. I noticed that with DOM, I can
only query specific elements, as there's no method like
getAllElements(). I really have to use SAX if I want this, right?

Does anybody know if the XML spec says anything about the order of
elements? Does the DOM API guarantee to return them in the same order as
they're encountered in the XML file?

Lack of a getAllElements() function makes my XML look like:

<xml>
<flow name="flow1">
<startnode name="start1" id="start1" nextnodeid="nn1"/>
<nullnode id="nn1" nextnodeid="flowlet1"/>
<flowlet id="flowlet1" type="PythonSnippetFlowlet" nextnodeid="fn1">
<param name="expression" expression="4+5"/>
</flowlet>
<forknode id="fn1" condition_nodeid="start1" alternative_nodeid="nn1" session_itemname="a" param1="b" condition="=="/> 
<stopnode id="sn1"/>
</flow>
</xml>

where I have to use an explicit attribute 'nextnodeid' to get an order
into the various elements. Probably not a big deal, as if this project
will ever get finished, there'll be a GUI editor for the flows.

Gerhard
-- 
This sig powered by Python!
Außentemperatur in München: 17.0 °C      Wind: 2.7 m/s