Reputation: 43
I have the following xml string and I want to get the value from ReturnCode in Python. How can I easily do this?
I've tried using element tree:
tree = ET.ElementTree(ET.fromstring(response))
root = tree.getroot()
<API>
<Result>
<ErrorCode ErrorType=\"Success\">0</ErrorCode>
<ReturnCode>0</ReturnCode>
</Result>
<API>
The actual response value looks like this-
'<API><Result><ErrorCode ErrorType=\"Success\">0</ErrorCode<ReturnCode>0</ReturnCode></Result><API>'
I would like to be able to use the value from ReturnCode for additional logic.
Upvotes: 2
Views: 7272
Reputation: 174
As official documents xml.etree.elementtree. Parse your xml document like this:
import xml.etree.ElementTree as ET
# root = ET.fromstring(your_xml_content)
# root.tag
body = '<API><Result><ErrorCode ErrorType="Success">0</ErrorCode><ReturnCode>0</ReturnCode></Result></API>'
response = ET.fromstring(body)
result = response.findall('Result')[0]
return_code = result.find('ReturnCode').text
## output '0'
Updated: I missed result
.
Upvotes: 2