Python 3 XML findall空输出

时间:2018-05-26 10:42:12

标签: python xml attributes elementtree findall

我试图找出以下问题:

我希望得到'类型'标签的属性(cpe-23标签是可选的,XML模式中的任何类型)

<cpe-23:deprecated-by ... type="NAME_CORRECTION"/>

在stackoverflow中阅读其他问题会让我看到以下代码:

import xml.etree.ElementTree as ET
tree = ET.parse(".../official-cpe-dictionary_v2.3.xml")
root = tree.getroot()

for child in root.findall('cpe-item/cpe-23:cpe23-item/cpe-23:deprecation/cpe-23:deprecated-by',
                      namespaces={'cpe-23': 'http://scap.nist.gov/schema/cpe-extension/2.3'}):

    print(child.attrib['name'], child.attrib['type'])

我现在的问题是我得到一个空输出,我的程序停止代码:0

关于如何解决这个问题的任何想法?

非常感谢提前!

来源(XML文件):

<cpe-list ...>
    <generator>
       ...
    </generator>
    <cpe-item name="cpe:/a:3com:tippingpoint_ips_tos:2.2.3" deprecated="true" deprecation_date="2010-12-28T17:36:02.240Z">
        <title xml:lang="en-US">3Com TippingPoint IPS TOS 2.2.3</title>
        <cpe-23:cpe23-item name="cpe:2.3:a:3com:tippingpoint_ips_tos:2.2.3:*:*:*:*:*:*:*">
          <cpe-23:deprecation date="2010-12-28T12:36:02.240-05:00">
            <cpe-23:deprecated-by name="cpe:2.3:o:3com:tippingpoint_ips_tos:2.2.3:*:*:*:*:*:*:*" type="NAME_CORRECTION"/>
          </cpe-23:deprecation>
        </cpe-23:cpe23-item>
    </cpe-item>
    <cpe-item ...>
        ...
    </cpe-item>
    ...
</cpe-list>

1 个答案:

答案 0 :(得分:0)

我能够自己阅读图书馆的文档。没有输出的原因是Element.findall()函数只查看direkt子节点。 “cpe-23:cp23-item”是根节点的孙子。