zoukankan      html  css  js  c++  java
  • 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 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'.
    esource	est.xml')

      

      

  • 相关阅读:
    jQuery Validate 插件
    jquery.validate.js校验select2解决方案,Jquery插件select2校验解决方案
    各大浏览器CSS Hack收集
    Jquery方法load之后导致js失效解决方法
    list-style无颜色问题解决,list-style-type无颜色解决
    Css实现透明效果,兼容IE8
    jdk配置及maven配置
    兼容IE浏览器的placeholder【超不错】
    laydate兼容bootstrap
    section和article元素
  • 原文地址:https://www.cnblogs.com/snow-backup/p/4026595.html
Copyright © 2011-2022 走看看