Python: Can iterate sub elements using elementTree

johndo

New Member
I have the following code to parse an XML but it just won't let me iterate through the children:\[code\]import urllib, urllib2, re, time, osimport xml.etree.ElementTree as ET def wgetUrl(target): try: req = urllib2.Request(target) req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3 Gecko/2008092417 Firefox/3.0.3') response = urllib2.urlopen(req) outtxt = response.read() response.close() except: return '' return outtxtnewUrl = 'http://feeds.rasset.ie/rteavgen/player/playlist?showId=10056467'data = http://stackoverflow.com/questions/14538678/wgetUrl(newUrl)tree = ET.fromstring(data)#tree = ET.parse(data)for elem in tree.iter('entry'): print elem.tag, elem.attrib\[/code\]Now, If I remove 'entry' from the iter I get an output like this (Why the URL??):\[code\]{http://www.w3.org/2005/Atom}entry {}{http://www.w3.org/2005/Atom}id {}{http://www.w3.org/2005/Atom}published {}{http://www.w3.org/2005/Atom}updated {}{http://www.w3.org/2005/Atom}title {'type': 'text'}\[/code\]But, If I put the iter statement like this it still does not find the children to entry:\[code\]for elem in tree.iter('{http://www.w3.org/2005/Atom}entry'): print elem.tag, elem.attrib\[/code\]I still only get the entry element on it's own, not the children:\[code\]{http://www.w3.org/2005/Atom}entry {}\[/code\]Any idea what I am doing wrong? I have searched everywhere but can't figure this out... I am new to all this so sorry if it is something stupid.
 
Back
Top