首页 > 代码库 > python 解析 xml
python 解析 xml
<taskList nextId="62292"><task module="reliability" owner="vprovodi" id="59074" status="error" result="XFL is OK;init OK;Tests: 17, Expected failures: 1, Unexpected: 1, Actual total count is OK: 17 >= 15(unmatched!);adb OK; Crashlogs: 0; Tombstones: 1; Sigs: 2+2+0+0; Throttlings: 0+0+0+0; Device errors: 0" addedBy="muxiaofx@muxiaofx-desk{client}" changedBy="muxiaofx@muxiaofx-desk{client}" runningBy="lab_labrqabj" runningOn="host007-agent11" addedTime="1410503955947" startTime="1410581427467" finishTime="1410591199376" > <match property="android.build.origin" value="ABT"/> <match property="android.build.target" value="KK"/> <match property="android.build.branch" value="art-opt"/> <match property="android.build.date" value="WW36"/> <match property="android.build.type" value="userdebug"/> <match property="android.device.type" value="T100TA"/> <match property="agent.group" value="art-opt"/> <property name="vm.backend" value="bronze"/> <property name="task.group" value="weekly_WW36_ABT_art-opt_bronze_T100TA"/> <property name="vm.mode" value="art"/> <property name="task.tests" value="zip_vm"/></task></taskList>
使用python 独有etree 方式解析
1 from xml.etree import ElementTree as et 2 3 def parseXml(filename=".\\resource\\test.xml"}): 4 result_list = [] 5 tree = et.parse(filename) 6 root = tree.getroot() 7 8 elements = root.findall("task") 9 for el in elements:10 adict = {}11 attr = el.attrib # attributes of task node12 adict.update(attr) # attr is a dict, put the key-values of attr into adict13 matches = el.findall(‘match‘)14 for m in matches:15 key = m.attrib.get(‘property‘)16 value = http://www.mamicode.com/m.attrib.get(‘value‘)17 adict[key] = value18 props = el.findall(‘property‘)19 for p in props:20 key = p.attrib.get(‘name‘)21 value = http://www.mamicode.com/p.attrib.get(‘value‘)22 adict[key] = value23 if is_target(adict, filter):24 result_list.append(adict)25 return result_list
使用 minidom 解析
1 from xml.dom.minidom import parse 2 3 def load_task_list(filename): 4 xml_dom = parse(filename) 5 node_tasklist = xml_dom.documentElement 6 7 for node_task in node_tasklist.getElementsByTagName(‘task‘): 8 for (attr_name, attr_value) in node_task.attributes.items(): 9 print attr_name, attr_value # attributes of task node10 for node in node_task.childNodes:11 if node.nodeType == node.ELEMENT_NODE:12 if node.nodeName == ‘match‘:13 prop_name = node.getAttribute(‘property‘)14 prop_value = http://www.mamicode.com/node.getAttribute(‘value‘)15 print ‘match: %s:%s‘%(prop_name, prop_value)16 elif node.nodeName == ‘property‘:17 prop_name = node.getAttribute(‘name‘)18 prop_value = http://www.mamicode.com/node.getAttribute(‘value‘)19 print ‘property: %s:%s‘%(prop_name, prop_value)20 21 load_task_list(r‘.\resource\test.xml‘)
python 解析 xml
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。