Reputation: 361
I have the following XML that I need parse completely including all the child nodes.
<?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>
I tried the following using xml.etree.cElementTree but it doesn't help me iterate through all the child nodes. I am sure there is a fairly simple solution to this
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
Upvotes: 0
Views: 53
Reputation: 365707
An XML tree is a tree, so you can use any tree algorithms on it. In particular, what you want here is a depth-first enumeration of all descendants, which is just about the simplest tree algorithm there is:
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
You can of course stick the print
statements directly in the loop instead of yielding the nodes, transform this from a recursive to an iterative implementation, convert the nested loop into a nested generator expression, etc., all without worrying about the fact that this happens to be an ElementTree
as opposed to some other kind of tree.
Upvotes: 1
Reputation: 89285
child in root
only iterate through direct children of the root node. To get all nodes within the root you can use xpath selector .//*
:
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
Upvotes: 1