1

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

2 Answers 2

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

http://docs.python.org/2/library/xml.etree.elementtree.html

Sign up to request clarification or add additional context in comments.

2 Comments

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'
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!!!
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.