解析XML的所有元素

时间:2015-04-24 01:02:51

标签: python xml

我有以下需要完全解析的XML,包括所有子节点。

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<UnitReport start-time="2014-12-10T08:22:48.296-07:00" end-time="2014-12-10T08:24:31.539-07:00" mode="Production">
  <Station guid="AC7D239F-C29A-49C3-B5D2-F0AE3867EBD8" name="STP-002" version="1.0">
    <Property>
      <ValueString name="Computer Name">STP-002</ValueString>
      <ValueString name="Test Stage">CFT</ValueString>
    </Property>
  </Station>
  <Operator name="Operator" />
  <Category name="kapp">
    <Product part-no="1023388" serial-no="F021436A500033" name="CCA, Touch Master Controller, KAPP42">
      <Property>
        <ValueString name="Product Revision">07</ValueString>
        <ValueString name="Software Version">1.0.8.1</ValueString>
        <ValueString name="Fixture Part Number">102470901</ValueString>
        <ValueString name="Fixture Serial Number">S011425TE00002</ValueString>
        <ValueString name="Test Stage">CFT</ValueString>
      </Property>
    </Product>
  </Category>
  <TestRun grade="PASS" end-time="2014-12-10T08:24:31.570-07:00" start-time="2014-12-10T08:22:48.327-07:00" name="1024710_DCBTouchMasterController_CFT">
    <TestRun grade="Done" end-time="2014-12-10T08:22:48.350-07:00" start-time="2014-12-10T08:22:48.347-07:00" name="Set Substage" />
    <TestRun grade="PASS" end-time="2014-12-10T08:22:56.546-07:00" start-time="2014-12-10T08:22:48.356-07:00" name="Setup Fixture">
      <Result>
          <ValueDouble usl="12" lsl="8" grade="PASS" uom="mA(Comp=GELE)" name="PS1_2 Current">9.5</ValueDouble>
          <ValueDouble usl="5.2" lsl="4.8" grade="PASS" uom="Volts(Comp=GELE)" name="PS1_2 Voltage">5</ValueDouble>
          <ValueDouble usl="40" lsl="0" grade="PASS" uom="mA(Comp=GELE)" name="PS1_1 Current">0.7</ValueDouble>
          <ValueDouble usl="3.4" lsl="3.1" grade="PASS" uom="Volts(Comp=GELE)" name="PS1_1 Voltage">3.3</ValueDouble>
          <ValueDouble usl="0" lsl="1" grade="PASS" uom="Boolean(Comp=EQ)" name="Fixture Closed">1</ValueDouble>
          <ValueDouble usl="0" lsl="5" grade="PASS" uom="(Comp=EQ)" name="Setup Fixture Total Measurements">5</ValueDouble>
          <ValueDouble usl="0" lsl="0" uom="(Comp=LOG)" name="Final Loop Count">1</ValueDouble>
        </Result>
    </TestRun>
    <TestRun grade="PASS" end-time="2014-12-10T08:23:03.727-07:00" start-time="2014-12-10T08:22:56.550-07:00" name="PowerUp Test">
      <Result>
          <ValueDouble usl="320" lsl="120" grade="PASS" uom="mA(Comp=GELE)" name="PS1_3 Current">272.6</ValueDouble>
          <ValueDouble usl="5.2" lsl="4.8" grade="PASS" uom="Volts(Comp=GELE)" name="PS1_3 Voltage">5</ValueDouble>
          <ValueDouble usl="0" lsl="2" grade="PASS" uom="(Comp=EQ)" name="PowerUp Test Total Measurements">2</ValueDouble>
        </Result>
    </TestRun>
    <TestRun grade="PASS" end-time="2014-12-10T08:23:05.845-07:00" start-time="2014-12-10T08:23:03.734-07:00" name="Voltage Rail Test">
      <Result>
          <ValueDouble usl="3.4" lsl="3.2" grade="PASS" uom="Volts(Comp=GELE)" name="VCC_3V3D(TP207)">3.311</ValueDouble>
          <ValueDouble usl="1.89" lsl="1.7" grade="PASS" uom="Volts(Comp=GELE)" name="VCC_1V8D(TP202)">1.812</ValueDouble>
          <ValueDouble usl="1.37" lsl="1.2" grade="PASS" uom="Volts(Comp=GELE)" name="VCC_1V3D(TP204)">1.286</ValueDouble>
          <ValueDouble usl="7.35" lsl="6.65" grade="PASS" uom="Volts(Comp=GELE)" name="VCC_7V0(TP218)">7.048</ValueDouble>
          <ValueDouble usl="1.26" lsl="1.1" grade="PASS" uom="Volts(Comp=GELE)" name="1V2_LDO(TP212)">1.197</ValueDouble>
          <ValueDouble usl="1.89" lsl="1.7" grade="PASS" uom="Volts(Comp=GELE)" name="1V8_LDO(TP213)">1.798</ValueDouble>
          <ValueDouble usl="0" lsl="1" grade="PASS" uom="Boolean(Comp=EQ)" name="SUPERVISOR_RESET">1</ValueDouble>
          <ValueDouble usl="0" lsl="7" grade="PASS" uom="(Comp=EQ)" name="Voltage Rail Test Total Measurements">7</ValueDouble>
          <ValueDouble usl="0" lsl="0" uom="(Comp=LOG)" name="Final Loop Count">1</ValueDouble>
        </Result>
      <Property>
        <ValueString name="Line">Report Text</ValueString>
        <ValueString name="01">VCC_3V3D(TP207), Max = 3.355, Min = 3.265, Avg = 3.311,</ValueString>
        <ValueString name="02">VCC_1V8D(TP202), Max = 1.822, Min = 1.802, Avg = 1.812,</ValueString>
        <ValueString name="03">VCC_1V3D(TP204), Max = 1.294, Min = 1.276, Avg = 1.286,</ValueString>
        <ValueString name="04">VCC_7V0(TP218), Max = 7.052, Min = 7.043, Avg = 7.048,</ValueString>
        <ValueString name="05">1V2_LDO(TP212), Max = 1.219, Min = 1.176, Avg = 1.197,</ValueString>
        <ValueString name="06">1V8_LDO(TP213), Max = 1.800, Min = 1.796, Avg = 1.798,</ValueString>
      </Property>
.
.
.
      </Property>
    </TestRun>
  </TestRun>
</UnitReport>

我使用xml.etree.cElementTree尝试了以下操作,但它无法帮助我遍历所有子节点。我相信这个

有一个相当简单的解决方案
import xml.etree.cElementTree as ET
tree = ET.ElementTree(file='test.xml')
root = tree.getroot()
#print tree.getroot()
for child in root:
        print child.tag, child.attrib

2 个答案:

答案 0 :(得分:1)

child in root仅迭代根节点的直接子。要获取根目录中的所有节点,可以使用xpath选择器.//*

import xml.etree.cElementTree as ET
tree = ET.ElementTree(file='test.xml')
root = tree.getroot()
for descendant in root.findall(".//*"):
    print descendant.tag, descendant.attrib

答案 1 :(得分:1)

XML树是一棵树,因此您可以在其上使用任何树算法。特别是,你想要的是depth-first enumeration of all descendants,这是最简单的树算法:

def descendants(tree):
    yield tree
    for child in tree:
        for child_descendant in descendants(child):
            yield child_descendant

for descendant in descendants(tree.getroot()):
    print descendant.tag, descendant.attrib

您当然可以直接在循环中粘贴print语句而不是产生节点,将其从递归实现转换为迭代实现,将嵌套循环转换为嵌套生成器表达式等,所有这些都没有担心事实上这恰好是ElementTree而不是其他某种树。