I finally got an XML parsing example working! I appreciate this is
very simple stuff but I am not a native Python programmer (apologies
for this).
At present, expat seems to be required to perform typical XML parsing
using minidom or ElementTree methods. However, ElementTree suggests
using a non-expat parser, namely SimpleXMLTreeBuilder.
As I could not find SimpleXMLTreeBuilder in my Python 2.5 installation
or in the Google App engine SDK I located SimpleXMLTreeBuilder using
Google Code Search (
http://www.google.com/codesearch?
q=SimpleXMLTreeBuilder).
Downloaded it, tweaked it slightly to replace "import ElementTree"
with "from xml.etree import ElementTree".
I then set about reproducing Yahoo! Developer Networks Weather example
from here:
http://developer.yahoo.com/python/python-xml.html
This is my code:
from google.appengine.api import urlfetch
from xml.etree import ElementTree
import SimpleXMLTreeBuilder
WEATHER_URL = '
http://xml.weather.yahoo.com/forecastrss?p=%s'
WEATHER_NS = '
http://xml.weather.yahoo.com/ns/rss/1.0'
def parse( url ) :
result = urlfetch.fetch(url)
if result.status_code == 200:
parser = SimpleXMLTreeBuilder.TreeBuilder()
parser.feed(result.content)
return parser.close()
def weather_for_zip(zip_code):
url = WEATHER_URL % zip_code
rss = parse(url)
forecasts = []
for element in rss.findall('channel/item/{%s}forecast' %
WEATHER_NS):
forecasts.append({
'date': element.get('date'),
'low': element.get('low'),
'high': element.get('high'),
'condition': element.get('text')
})
ycondition = rss.find('channel/item/{%s}condition' % WEATHER_NS)
return {
'current_condition': ycondition.get('text'),
'current_temp': ycondition.get('temp'),
'forecasts': forecasts,
'title': rss.findtext('channel/title')
}
print 'Content-Type: text/plain'
print ''
print weather_for_zip('94089')
and it works!
http://yahoo-weather.appspot.com/
Hope this helps!
Mark