I'm trying to search for something on Pastebin, via their API. I'm searching using the pastebin library for python.
The problem is that I receive an XML response, that has repeating keys.
This is the response
<paste>
<paste_key>fadsda</paste_key>
<paste_date>1409074286</paste_date>
<paste_title>badPaste</paste_title>
<paste_size>2040</paste_size>
<paste_expire_date>0</paste_expire_date>
<paste_private>0</paste_private>
<paste_format_long>Bash</paste_format_long>
<paste_format_short>bash</paste_format_short>
<paste_url>http://pastebin.com/url2</paste_url>
<paste_hits>211</paste_hits>
</paste>
<paste>
<paste_key>fsfgdsgg</paste_key>
<paste_date>1398409838</paste_date>
<paste_title>goodPaste</paste_title>
<paste_size>2407</paste_size>
<paste_expire_date>0</paste_expire_date>
<paste_private>2</paste_private>
<paste_format_long>Bash</paste_format_long>
<paste_format_short>bash</paste_format_short>
<paste_url>http://pastebin.com/otherURL</paste_url>
<paste_hits>54</paste_hits>
</paste>
So I am trying to parse it to return paste_key
when paste_title == goodPaste
, but the attrib is always empty
def parseXML(response):
#I'm adding a root tag
xml = ElementTree.fromstring('<list>' + response + '</list>')
for child in root:
for elem in child:
print elem.tag, elem.attrib
returns
paste_key {}
paste_date {}
paste_title {}
paste_size {}
paste_expire_date {}
paste_private {}
paste_format_long {}
paste_format_short {}
paste_url {}
paste_hits {}
paste_key {}
paste_date {}
paste_title {}
paste_size {}
paste_expire_date {}
paste_private {}
paste_format_long {}
paste_format_short {}
paste_url {}
paste_hits {}
EDIT:
So I was supposed to use elem.text, so that's working now, but the main question still exists:
How can I return the element where paste_key
when paste_title == goodPaste
EDIT 2 Winning ticket:
result = xml.findall(".//paste[paste_title='goodPaste']/paste_key")
print result[0].text
You can use XPath for that:
result = xml.findall(".//paste[paste_title='goodPaste']/paste_key")
print result.text
This should print fsfgdsgg
in your case