我有一个XML字符串,我从包含状态信息的API收到。 XML中可以有不同的元素,具体取决于操作是否成功。
一个例子如下:
<?xml version="1.0" encoding="UTF-8"?>
<content>
<status>ok</status>
<response>ok</response>
<jobid>[integer]</jobid>
</content>
或可能:
<?xml version="1.0" encoding="UTF-8"?>
<content>
<status>fail</status>
<error>missingdata</error>
</content>
我正在尝试使用ElementTree来解析XML数据并提取每个项的值,将它们转换为字符串(如果需要),保存到变量并将该数据写入日志文件。抛弃我的部分是我无法确定那里会有什么元素。
我一直在使用此链接作为指导: http://docs.python.org/2/library/xml.etree.elementtree.html
答案 0 :(得分:0)
尝试使用try / except:
import xml.etree.ElementTree as ET
from lxml import etree
tree = ET.parse('test.xml')
root = tree.getroot()
for child in root:
try:
x = child.find('response').text
except AttributeError:
continue
如果有response
元素,x将给出该值,否则跳过它。将其用于所有可能的元素
答案 1 :(得分:0)
这是一个从xml数据中读取所有现有元素并将它们存储在字典中的示例:
import xml.etree.ElementTree as et
xml1="""<?xml version="1.0" encoding="UTF-8"?>
<content>
<status>ok</status>
<response>ok</response>
<jobid>[integer]</jobid>
</content>
"""
xml2="""<?xml version="1.0" encoding="UTF-8"?>
<content>
<status>fail</status>
<error>missingdata</error>
</content>
"""
def get_elements(xml_input):
ret = {}
tree = et.fromstring(xml_input)
for el in tree:
ret[el.tag] = el.text
return ret
print get_elements(xml1)
print get_elements(xml2)
答案 2 :(得分:0)
我建议我们创建一个简单的类来解析XML,将所有二级节点转换为对象属性。然后,我们可以检查状态是确定还是失败,并采取相应措施。这是一个示例:
import xml.etree.ElementTree as ET
class XmlResponse(object):
'''
A simple class to parse a response XML
'''
def parseString(self, xml_string):
'''
Parses an XML block of text and convert all secondary nodes into
attributes for this object.
'''
root = ET.fromstring(xml_string)
for node in root:
setattr(self, node.tag, node.text)
def parse(self, filename):
'''
Parses an XML file and convert all secondary nodes into attributes for
this object.
'''
with open(filename) as f:
self.parseString(f.read())
def interpret_response(filename):
'''
A simple demo of how to use the XMLResponse class
'''
response = XmlResponse()
response.parse(filename)
if response.status == 'ok':
print 'Job ID:', response.jobid
else:
print 'Error:', response.error
if __name__ == '__main__':
interpret_response('ok.xml')
interpret_response('fail.xml')
为简单起见,我不检查错误(例如找不到文件),但你应该这样做。