XML-code examples

Paul Prescod paul at prescod.net
Thu Jun 22 14:58:52 EDT 2000


Thomas Weholt wrote:
> 
> Hi,
> 
> Is there more simple XML code out there? I mean the HowTos took about
> ten minutes, and it didn`t cover much.
> 
> If somebody has some tips or code on how to traverse a DOM-tree,
> processing nodes based on tagname, and especially, based on relations
> with other nodes ( i.e. who`s your daddy, here`s your sister etc ),
> that would be great.

Well, at its simplest, you can do something like this:

for node in root.getElementsByTagName( "*" ):
    if node.tagName=="...": doSomething()
    elif node.tagName=="...": doSomethingElse()
    elif node.tagName=="...": doSomethingElseAgain()

I am trying to get people to test out and "vote" for a new library that
I want to put into Python 1.6. It is half-way between SAX and DOM. It is
efficient like SAX because you only ever work with a "peep hole" of your
document. It is convenient like DOM because you have access to your
parent nodes (but not your sibling nodes). If you want access to your
child nodes, you can ask it, but you are responsible for the performance
implications for building a tree (e.g. you don't want to do it on the
root of a 1GB document).

If you have a little time to experiment with something that may be part
of Python soon (or in the distant future...depending) then I would
appreciate you trying it out and seeing if it solves your problem as
conveniently and efficiently as I hope it will.

http://www.prescod.net/python/pulldom.html
-- 
 Paul Prescod  - ISOGEN Consulting Engineer speaking for himself
"So there you have it folks, the Millenium: warring and whoring but
never boring." - Dennis Miller




More information about the Python-list mailing list