我决定最好有一种方法可以从XML
字符串中创建字典。
import xml.etree.ElementTree as ET
response = '<some><generic><xml></xml></generic></some>'
targetTree = ET.fromstring(response)
# Do something cool here
for key in my_cool_dict:
print '{0} = {1}'.format(key, my_cool_dict[key])
我找到了迭代元素的好方法:
for elem in targetTree.findall('some'):
for child in elem.getchildren():
print i.text
这类事情需要我知道XML
标签。如果我不知道我收到了什么怎么办?如何创建标签名称和标签之间的值my_cool_dict
?key
value