views:

24

answers:

1

I'm trying to read through an xml feed I'm getting, but I can't access the specific elements. I'm using python, and the python documentation is really unclear about what I should use.

Here is the feed:

<title>More eagle</title>
<summary>http://www.181.fm/winamp.plsstation=181eagle&amp;amp;style=&amp;amp;description=The%20Eagle%20(Classic ...</summary> 
<link rel="alternate" href="http://mail.google.com/mail [email protected]&amp;message_id=12995390f36c310b&amp;view=conv&amp;extsrc=atom" type="text/html" />
<modified>2010-07-02T22:13:51Z</modified>
<issued>2010-07-02T22:13:51Z</issued>
<id>tag:gmail.google.com,2004:1340194246143783179 </id>

And here is my current function:

def parse_xml(feed):
    feedxml = minidom.parseString(feed)
    name = feedxml.getElementsByTagName('name')
    subject = feedxml.getElementsByTagName('title')
    contents = feedxml.getElementsByTagName('summary')
    return name + "\n" + subject + "\n" + contents
A: 
getElementsByTagName()

returns a list of elements. So if you want the first (or only) one, you need to use getElementsByTagName('name')[0].

But this is an element object, not the text enclosed by it (which I presume you're interested in).

So you probably need to do something like this:

nametag = feedxml.getElementsByTagName('name')[0]
nametag.normalize()
name = nametag.firstChild.data
Tim Pietzcker
That worked! Thank you.
SachaK