我使用以下代码来解析XML
from xml.etree.ElementTree import iterparse
# Read xml file
configTree = iterparse('../../SamdConfigurationMainEndat_Hi_LUT.xml', ['start', 'end'])
depth = 0
skipNode = False
parseNodes = ['Actuator', 'Sensor', 'AxisControl', 'ControlNetwork']
level = []
signals = []
for (event, node) in configTree:
n = n + 1
if event == 'end':
if node.tag == 'Alignment':
skipNode = False
print(node.tag, signals[-3:-1])
if node.tag in parseNodes:
level.pop()
depth -= 1
# skip alignment node and only process when node start
if skipNode is False and event == 'start':
# check only parseNodes tag and level information
if node.tag in parseNodes:
# add elem level information
if node.get('Name'):
level.append(node.get('Name'))
else:
level.append(node.tag)
print(depth*'*', node.tag, skipNode, event, level)
elif node.tag == 'Signal':
signalValue = {node.get('Name'): node.text}
elem = [level, signalValue]
signals.append(elem)
print(depth*'=', node.tag, level, signalValue)
else:
print(depth*'+', node.tag)
if event == 'start':
if node.tag == 'Alignment':
skipNode = True
depth += 1
输出存储在信号列表中。 信号列表的输出不包含级别信息,但在执行代码期间在print语句中可见。
是什么导致关卡信息从信号列表中消失?