XML help

Diez B. Roggisch deets at web.de
Sun Jun 5 16:34:40 CEST 2005

> I've managed to test for specific elements and extract values. I want to
> place the reults in arrays with array index equal to element ID. So as I
> walk the tree I temporarily store IDs and DeptValues in lists. I'm ok so
> far. I then intend to create an array of size determined by the maximum
> value of ID. So in the sample above the array size will be 8 even though
> only three entries exist.

That sounds wrong. If what you want is a mapping between keys (your IDs) 
and values, you need to use a dcitionary. Like this:

mapping[myid] = value

> At this point I'm stuck because I want to do this latter array creation and
> processing when I "see" the /Block end of block tag. However I can't figure
> out how to do that. Obviously I'm not understanding something about XML DOM
> trees and Elements because when I try to print all elements I never see an
> end tag for any. I'm obviously approaching this from a readline and process
> point of view which is probably half the problem.

Your misunderstanding the nature of nodes in dom: a node in dom _is_ the 
start and end-tag. If you have a dom-tree that you serialize, a node in 
there will either be serialized as


if it has no childs, or as


if it has childs. So the other way round, for a well-formed xml document 
   parsed to a dom, you end up with one node for all pairs of 
opening/closing tags.

If a end-tag is what you're after, you migth want to look into the 
event-driven XML api, SAX. But then you'll have other tradeoffs compared 
to dom': all statekeeping has to be done by yourself. It's up to you to 



More information about the Python-list mailing list