Reputation: 131
I am getting an xml response containing data from a web site. I want to pick out the data within specific xml tags <dcdIntegerValue>531</dcdIntegerValue>
and assign it to a variable. Currently I'm using "response_body" to get said response output. I am using python 2.7 and httplib if that matters. I'm not sure where to find information on how to do this. Any help is appreciated.
Thanks
Mike
Upvotes: 1
Views: 1371
Reputation: 63
You either can use pythons ElementTree:
edit: i forgot every item in this list is a type of element. You should get the contents by instead using:
number = int(item.text)
import xml.etree.ElementTree as et
xmltree = et.ElementTree(file="yourxmlfile.xml")
intList = xmltree.getroot().iterfind(".//dcdIntegerValue")
for item in intList:
number =int(item.text)
#do whatever you want with your number
http://docs.python.org/2/library/xml.etree.elementtree.html
Upvotes: 4
Reputation: 245
Why don't use a regex ?
import re
import urllib
stream=urllib.urlopen("http://....")
result=re.search('<dcdIntegerValue>(\d*)</dcdIntegerValue>',stream.read(),re.IGNORECASE)
if result:
value=result.group(1)
Maybe a little "brutal" but i think it works.
Inspired by : extract contents of regex
Upvotes: 2