[Tutor] Extracting xml text

T.R. D. trdillah at gmail.com
Sun Jun 20 08:03:00 CEST 2010


Hi,

I'm trying to parse a list of xml strings and so far it looks like the
xml.parsers.expat is the way to go but I'm not quite sure how it works.

I'm trying to parse something similar to the following.  I'd like to collect
all headings and bodies and associate them in a variable (dictionary for
example). How would I use the expat class to do this?

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

<note>
<to>Jani</to>
<from>Tovi</from>
<heading>Reminder 2</heading>
<body>Don't forget to bring snacks!</body>
</note>

....

Here's the code that I have so far based on the documents I've read:

actions = test.actions


# Parse stepgreen actions

parser = xml.parsers.expat.ParserCreate()

parser.Parse(reminderXML)
print parser.StartElementHandler;

This doesn't work.... (even if there's one item in the list).

Could someone please point me in the right direction? I haven't found any
examples of what I'd like to do and I don't understand how expat works.
Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100620/a8ccbe5c/attachment.html>


More information about the Tutor mailing list