On Tuesday, May 24, 2011 11:13:31 AM UTC+1, Lloyd Dube wrote: > > Hi Everyone, > > I am trying to parse an XML feed and display the text of each child node > without any success. My code in the python shell is as follows: > > >>>import urllib > >>>from xml.etree import ElementTree as ET > > >>>content = urllib.urlopen(' > http://xml.matchbook.com/xmlfeed/feed?sport-id=&vendor=TEST&sport-name=&short-name=Po > ') > >>>xml_content = ET.parse(content) > > I then check the xml_content object as follows: > > >>>xml_content > <xml.etree.ElementTree.ElementTree instance at 0x01DC14B8> > > And now, to iterate through its child nodes and print out the text of each > node: > > >>>for node in xml_content.getiterator('contest'): > ... name = node.attrib.get('text') > ... print name > ... > >>> > > Nothing is printed, even though the document does have 'contest' tags with > text in them. If I try to count the contest tags and increment an integer > (to see that the document is traversed) I get the same result - the int > remains at 0. > > >>> i = 0 > >>> for node in xml_content.getiterator('contest'): > ... i += 1 > ... > >>> i > 0 > > What am I getting wrong? Any hints would be appreciated. > > -- > Regards, > Sithembewena Lloyd Dube
This isn't really a Django question... Nevertheless, the issue is probably in the line "name = node.attrib.get('text')". What this does is get the attribute of the current node that has the name 'text' - ie if your XML was like this: <contest text="foo"/> However, what you probably have is this: <contest>foo</contest> in which case you just want to access the `text` property directly: name = node.text -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.