a few more questions on XML and python

Martin v. Loewis martin at v.loewis.de
Thu Jan 3 21:38:32 EST 2002


Rajarshi Guha <rxg218 at psu.edu> writes:

> How can I  indicate to the parser that if say it comes upon a tag like 
> <coordinate> it should call a certain handler to process the coordinate 
> data.
> 
> Or am I supposed to just do the handling by extractin the information 
> provided by expat. If so is there any type of 'hook' system where I can 
> plug in my handlers.

If you want to use expat directly, you need to get used to the notion
of event-based processing. The parser invokes a callback (aka handler)
for each chunk of XML input. That callback must perform all the
processing.  It will invoke the same callback for all opening tags, so
inside this callback, you must look at the element name. If it is
coordinate, you set a global variable remembering that you have just
seen the <coordinate> tag. 

Each of the other callbacks needs to look at the global variable. If
it is set, do everything  needed inside the coordinate.

If the end-element callback is invoked, and the tag is coordinate,
clear the global variable.

Alternatively, you may want to look at the DOM API. There, the parser
reads the entire document first, and gives you a tree structure. You
can then traverse this tree structure as you want.

Regards,
Martin




More information about the Python-list mailing list