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
Asked
Active
Viewed 1,348 times
1

Michael Emond
- 131
- 10
2 Answers
4
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

nakih
- 63
- 6
-
1Thanks 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 Jan 28 '14 at 16:55
-
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!!! – Michael Emond Jan 29 '14 at 14:55
2
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