码迷,mamicode.com
首页 > 编程语言 > 详细

python 解析 xml

时间:2014-10-15 17:23:31      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   blog   color   os   使用   ar   for   

 

<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 node
12         adict.update(attr) # attr is a dict, put the key-values of attr into adict
13         matches = el.findall(match)
14         for m in matches:
15             key = m.attrib.get(property)
16             value = m.attrib.get(value)
17             adict[key] = value
18         props = el.findall(property)
19         for p in props:
20             key =  p.attrib.get(name)
21             value = p.attrib.get(value)
22             adict[key] = value
23         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 node
10         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 = 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 = node.getAttribute(value)
19                     print property: %s:%s%(prop_name, prop_value)
20 
21 load_task_list(r.\resource\test.xml)

 

  

  

python 解析 xml

标签:des   android   style   blog   color   os   使用   ar   for   

原文地址:http://www.cnblogs.com/snow-backup/p/4026595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!