How to read between xml tags?

Cameron Laird claird at lairds.com
Sun Mar 14 20:03:08 CET 2004


In article <mailman.251.1078956407.19534.python-list at python.org>,
Anthony Liu  <antonyliu2002 at yahoo.com> wrote:
>Yes, Miki, your code works great to strip the XML tags
>and return a clean text file.
>
>But the thing is, I want to process the part between
>tags each time it is read in.
>
>For example if I have a tagged XML doc like so:
>
><tag1>Something here</tag1>
><tag2>something else here</tag2>
>
>I want to get "Something here" in one read operation
>and process it before I move on to get "Something else
>here".
>
>So any way to go about this?
		.
		.
		.
>> I think the xml.sax module is what you're looking
>> for.
>> A small, briefly tested something might be:
>> ---
>> #!/usr/bin/env python
>> 
>> from xml.sax.handler import ContentHandler
>> from xml.sax import parse
>> 
>> class ArticleHandler(ContentHandler):
		.
		.
		.
If I understand you, that's *exactly* what Miki has provided
you.  His code reads up until the first tag is closed, then
hands over control to the "callback" he's provided as an 
example.  If you want something different to happen, just
substitute in your own action.
-- 

Cameron Laird <claird at phaseit.net>
Business:  http://www.Phaseit.net



More information about the Python-list mailing list