1
我正在处理一个程序生成的具有糟糕标签的xml片段。我能够解析出我在寻找的东西,但我认为这不是pythonic。该计划的目标是总计总线条目等。我使用elementtree来提取值。 寻找简化我的语法的建议。使用xml子对象,Python中的ElementTree
XML:
<?xml version="1.0"?>
<Status name="System Status">
<Bean name="SLMHandler" cn="com.open.eventCollector.OSLMHandler">
<Status name="SLF File manager for SODLC2">
<Prop name="totalLines">1105413065</Prop>
</Status>
</Bean>
<Bean name="ThreadPool" cn="com.open.util.OThreadPoolBean">
<Prop name="minThreads">5</Prop>
<Prop name="maxThreads">25</Prop>
<Prop name="checkedOutThreads">3</Prop>
<Prop name="availableThreads">2</Prop>
<Prop name="maxIdleTime">300000</Prop>
</Bean>
<Bean name="EventCollector" cn="com.open.eventCollector.OEventCollector">
<Prop name="numUnmatchedLines">785319</Prop>
<Status name="Adapters">
<Status name="Logfile_EpilogWin_Generic">
<Prop name="linesRead">0</Prop>
</Status>
</Status>
</Bean>
的Python:
import xml.etree.cElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
for bean in root.findall('Bean'):
for status in bean.findall('Status'):
if 'Adapters' in status.get('name'):
for status2 in status.findall('Status'):
for prop in status2.findall('Prop'):
if 'linesRead' in prop.get('name'):
print prop.text
这很好,不幸的是我正在使用的服务器完全与互联网隔离,只有python 2.7显然不支持xpaths。 –