Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中使用xml子对象ElementTree_Python_Xml_Xml Parsing_Elementtree - Fatal编程技术网

在Python中使用xml子对象ElementTree

在Python中使用xml子对象ElementTree,python,xml,xml-parsing,elementtree,Python,Xml,Xml Parsing,Elementtree,我正在使用一个程序生成的xml片段,该片段具有糟糕的标记。我能够解析出我在寻找什么,但我不认为这是非常蟒蛇。该计划的目标是合计Totalines条目,以及其他条目。我使用elementtree来提取这些值。 寻找关于简化语法的建议 xml: 不确定要如何打印最终结果,但可以使用单个XPath表达式一次性打印: 为您的示例数据打印0。这非常好,不幸的是,我使用的服务器与internet完全隔离,只有python 2.7不支持XPath。 <?xml version="1.0"?> &l

我正在使用一个程序生成的xml片段,该片段具有糟糕的标记。我能够解析出我在寻找什么,但我不认为这是非常蟒蛇。该计划的目标是合计Totalines条目,以及其他条目。我使用elementtree来提取这些值。 寻找关于简化语法的建议

xml:


不确定要如何打印最终结果,但可以使用单个XPath表达式一次性打印:


为您的示例数据打印
0

这非常好,不幸的是,我使用的服务器与internet完全隔离,只有python 2.7不支持XPath。
<?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>
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
for lines in root.findall('.//Bean/Status[@name="Adapters"]/Status/Prop[@name="linesRead"]'):
    print(lines.text)