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
Add a comment
|
2 Answers
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
2 Comments
Michael Emond
Thanks for your response. Do you know why I would be getting the error "TypeError: int() argument must be a string or a number, not 'Element'
Michael Emond
Excellent. Thanks for your edit. That worked for me as well. Already marked the other one as the answer so I'll give your's a +1. Thanks!!!
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