Wednesday, May 25, 2011

Re: Parsing an XML feed using ElementTree

P.S: I was aware that I posted a non-django question: I just took the chance that someone here may have needed to do the same.

Thanks!

On Wed, May 25, 2011 at 2:35 PM, Sithembewena Lloyd Dube <zebra05@gmail.com> wrote:
Hi Everyone,

Thanks for all your suggestions. I read up on gzip and urllib and also learned in the process that I could use urllib2 as its the latest form of that library.

Herewith my solution: I don't know how elegant it is, but it works just fine.

def get_contests():
     url = 'http://xml.matchbook.com/xmlfeed/feed?sport-id=&vendor=TEST&sport-name=&short-name=Po'
     req = urllib2.Request(url)
     req.add_header('accept-encoding','gzip/deflate')
     opener = urllib2.build_opener()
     response = opener.open(req)
     compressed_data = response.read()
     compressed_stream = StringIO.StringIO(compressed_data)
     gzipper = gzip.GzipFile(fileobj=compressed_stream)
     data = gzipper.read()
     current_path = os.path.realpath(MEDIA_ROOT + '/xml-files/d.xml')
     data_file = open(current_path, 'w')
     data_file.write(data)
     data_file.close()
     xml_data = ET.parse(open(current_path, 'r'))
     contest_list = []
     for contest_parent_node in xml_data.getiterator('contest'):
          contest = Contest()
          for contest_child_node in contest_parent_node:
               if (contest_child_node.tag == "name" and contest_child_node.text is not None and contest_child_node.text != ""):
                    contest.name = contest_child_node.text
               if (contest_child_node.tag == "league" and contest_child_node.text is not None and contest_child_node.text != ""):
                   contest.league = contest_child_node.text
               if (contest_child_node.tag == "acro" and contest_child_node.text is not None and contest_child_node.text != ""):
                   contest.acro = contest_child_node.text
               if (contest_child_node.tag == "time" and contest_child_node.text is not None and contest_child_node.text != ""):
                   contest.time = contest_child_node.text
               if (contest_child_node.tag == "home" and contest_child_node.text is not None and contest_child_node.text != ""):
                   contest.home = contest_child_node.text
               if (contest_child_node.tag == "away" and contest_child_node.text is not None and contest_child_node.text != ""):
                   contest.away = contest_child_node.text
          contest_list.append(contest)
     try:
          os.remove(current_path)
     except:
          pass
     return contest_list

Many thanks!





On Tue, May 24, 2011 at 10:26 PM, Brian Bouterse <bmbouter@gmail.com> wrote:
We all have our opinions.  Either way this conversation is OT from Django.


On Tue, May 24, 2011 at 4:07 PM, Masklinn <masklinn@masklinn.net> wrote:
On 2011-05-24, at 21:57 , Brian Bouterse wrote:
> +1 for xpath
>
> I also like using
> xml.dom.minidom<http://docs.python.org/library/xml.dom.minidom.html>since
> it is so simple and straightforward.
>
I'm sorry, but I whole-heartedly disagree with this. ElementTree is orders of magnitude simpler and more straightforward than the unending pain of working with the DOM interface.

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Brian Bouterse
ITng Services

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Regards,
Sithembewena Lloyd Dube



--
Regards,
Sithembewena Lloyd Dube

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment