Fwd: parsing RSS XML feed for item value

Neil Cerutti mr.cerutti at gmail.com
Wed Nov 20 15:48:52 CET 2013

Larry Wilson itdlw1 at gmail.com via python.org
10:39 PM (10 hours ago) wrote:
> Wanting to parse out the the temperature value in the
> "<w:current" element, just after the guid element using
> ElementTree or xml.sax.

Since you aren't building up a complex data structure, xml.sax
will be an OK choice.

Here's a quick and dirty job:

import io
import xml.sax as sax

the_xml = io.StringIO("""SNIPPED XML""")

class WeatherHandler(sax.handler.ContentHandler):
    def startDocument(self):
        self.temperatures = []

    def startElement(self, name, attrs):
        if name == 'w:current': # Nice namespace handling, eh?

handler = WeatherHandler()
sax.parse(the_xml, handler)
for temp in handler.temperatures:
    for key, val in temp.items():
        print("{}: {}".format(key, val))

Output (from your example):

windGusts: 29.6
dewPoint: 18.6
pressure: 0.0
windDirection: SSW
humidity: 90
rain: 0.6
temperature: 20.3
windSpeed: 22.2

For most jobs you would want to keep track of your nesting level, but
that's left out here. I didn't try to capture location or info you
might want but didn't specify, either; left that as an exercise.

More information about the Python-list mailing list