Splitting SAX results

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Jun 12 21:01:48 EDT 2007


En Tue, 12 Jun 2007 16:16:45 -0300, IamIan <iansan at gmail.com> escribió:

> I do know how split works, but thank you for the response. The end
> result that I want is a dictionary made up of the title results coming
> through SAX, looking like {'Title1: Description',
> 'Title2:Description'}.
>
> The XML data looks like:
> <item>
> <title>Title1:Description</title>
> <link>Link</link>
> <description>Desc</description>
> <author>Author</author>
> <pubDate>Date</pubDate>
> </item>
> <item>
> <title>Title2:Description</title>
> <link>Link</link>
> <description>Desc</description>
> <author>Author</author>
> <pubDate>Date</pubDate>
> </item>
>
> I've tried different approaches, a couple of which I've added to the
> code below (only running one option at a time):

Forget about SAX. Use ElementTree instead

py> import xml.etree.cElementTree as ET
py> f = open("x.xml","r")
py> tree = ET.parse(f)
py> for item in tree.getiterator('item'):
...   print item.findtext('title')
...
Title1:Description
Title2:Description

ElementTree is infinitely more flexible and easier to use.
See <http://effbot.org/zone/element-index.htm>

-- 
Gabriel Genellina




More information about the Python-list mailing list