如何使用 Python ElementTree 在 XML 解析期间检查条件

自动学习器

我正在尝试使用 ElementTree 解析 XML 并获取所有必需的字段。

问题:我的列表变空了,我尝试的条件是 - 如果 reference('type') == 'cve' 那么我想在参考标签中获取 'id' 文本。

有人可以建议/纠正我获得必填字段吗?

我的实际代码如下:

import xml.etree.ElementTree as ET

file_name = "updateinfo.xml"
parser = ET.XMLParser(encoding="utf-8")
tree = ET.parse(file_name, parser=parser)
tree_toString = (ET.tostring(tree.getroot()))
for ele in tree.findall('update'):
    cveList = [
        ele.find('references/reference').get('id') if ele.find('references/reference').get('type') == 'cve' else None
        for cve in ele.find('references/reference')]
    print cveList

我的 XML 结构如下:

<?xml version="1.0" encoding="UTF-8"?>
<updates>
        <update status="final" from="[email protected]" version="4" type="enhancement" >
            <id>RHEA-2017:2259</id>
            <issued date="2017-08-01 05:59:34 UTC" />
            <title>new packages: usbguard</title>
            <release>0</release>
            <rights>Copyright 2017 Red Hat Inc</rights>
            <pushcount>4</pushcount>
            <updated date="2017-08-01 05:59:34 UTC" />
            <references>
                <reference href="https://access.redhat.com/errata/RHEA-2017:2259" type="self" id="RHEA-2017:2259" title="RHEA-2017:2259" />
                <reference href="https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/7.4_Release_Notes/index.html" type="other" id="ref_0" title="other_reference_0" />
            </references>
            <pkglist>
                <collection short="" >
                    <name>rhel-7-server-rpms__7_DOT_4__x86_64</name>
                    <package src="usbguard-0.7.0-3.el7.src.rpm" name="usbguard" epoch="0" version="0.7.0" release="3.el7" arch="i686" >
                        <filename>usbguard-0.7.0-3.el7.i686.rpm</filename>
                        <sum type="sha256" >efd5ca6dd3df02e8537cf45cef48508bf023f568a98ce9f28e9baf77c5caac6c</sum>
                    </package>
                    <package src="usbguard-0.7.0-3.el7.src.rpm" name="usbguard" epoch="0" version="0.7.0" release="3.el7" arch="x86_64" >
                        <filename>usbguard-0.7.0-3.el7.x86_64.rpm</filename>
                        <sum type="sha256" >3f72768880085d6bfff37636d3a8eb54184e5619353b5efbefd5738e74bdfa08</sum>
                    </package>
                </collection>
            </pkglist>
        </update>
        <update status="final" from="[email protected]" version="1" type="bugfix" >
            <id>RHBA-2014:0722</id>
            <issued date="2014-06-10 00:00:00" />
            <title>kexec-tools bug fix update</title>
            <rights>Copyright 2014 Red Hat Inc</rights>
            <pushcount>1</pushcount>
            <updated date="2014-06-10 00:00:00" />
            <references>
                <reference href="https://rhn.redhat.com/errata/RHBA-2014-0722.html" type="self" title="RHBA-2014:0722" />
            </references>
            <pkglist>
                <collection short="" >
                    <name>rhel-7-server-rpms__7_DOT_4__x86_64</name>
                    <package src="kexec-tools-2.0.4-32.el7_0.1.src.rpm" name="kexec-tools" epoch="0" version="2.0.4" release="32.el7_0.1" arch="x86_64" >
                        <filename>kexec-tools-2.0.4-32.el7_0.1.x86_64.rpm</filename>
                        <sum type="sha256" >8e214681104e4ba73726e0ce11d21b963ec0390fd70458d439ddc72372082034</sum>
                    </package>
                </collection>
            </pkglist>
        </update>
        <update status="final" from="[email protected]" version="4" type="security" >
            <id>RHSA-2017:2831</id>
            <issued date="2017-09-28 18:56:55 UTC" />
            <title>Critical: firefox security update</title>
            <release>0</release>
            <rights>Copyright 2017 Red Hat Inc</rights>
            <severity>Critical</severity>
            <pushcount>4</pushcount>
            <updated date="2017-09-28 18:56:56 UTC" />
            <references>
                <reference href="https://access.redhat.com/errata/RHSA-2017:2831" type="self" id="RHSA-2017:2831" title="RHSA-2017:2831" />
                <reference href="https://bugzilla.redhat.com/show_bug.cgi?id=1496649" type="bugzilla" id="1496649" title="CVE-2017-7793 Mozilla: Use-after-free with Fetch API (MFSA 2017-22)" />
                <reference href="https://bugzilla.redhat.com/show_bug.cgi?id=1496651" type="bugzilla" id="1496651" title="CVE-2017-7810 Mozilla: Memory safety bugs fixed in Firefox 56 and Firefox ESR 52.4 (MFSA 2017-22)" />
                <reference href="https://bugzilla.redhat.com/show_bug.cgi?id=1496652" type="bugzilla" id="1496652" title="CVE-2017-7814 Mozilla: Blob and data URLs bypass phishing and malware protection warnings (MFSA 2017-22)" />
                <reference href="https://bugzilla.redhat.com/show_bug.cgi?id=1496653" type="bugzilla" id="1496653" title="CVE-2017-7818 Mozilla: Use-after-free during ARIA array manipulation (MFSA 2017-22)" />
                <reference href="https://bugzilla.redhat.com/show_bug.cgi?id=1496654" type="bugzilla" id="1496654" title="CVE-2017-7819 Mozilla: Use-after-free while resizing images in design mode (MFSA 2017-22)" />
                <reference href="https://bugzilla.redhat.com/show_bug.cgi?id=1496655" type="bugzilla" id="1496655" title="CVE-2017-7823 Mozilla: CSP sandbox directive did not create a unique origin (MFSA 2017-22)" />
                <reference href="https://bugzilla.redhat.com/show_bug.cgi?id=1496656" type="bugzilla" id="1496656" title="CVE-2017-7824 Mozilla: Buffer overflow when drawing and validating elements with ANGLE (MFSA 2017-22)" />
                <reference href="https://www.redhat.com/security/data/cve/CVE-2017-7793.html" type="cve" id="CVE-2017-7793" title="CVE-2017-7793" />
                <reference href="https://www.redhat.com/security/data/cve/CVE-2017-7810.html" type="cve" id="CVE-2017-7810" title="CVE-2017-7810" />
                <reference href="https://www.redhat.com/security/data/cve/CVE-2017-7814.html" type="cve" id="CVE-2017-7814" title="CVE-2017-7814" />
                <reference href="https://www.redhat.com/security/data/cve/CVE-2017-7818.html" type="cve" id="CVE-2017-7818" title="CVE-2017-7818" />
                <reference href="https://www.redhat.com/security/data/cve/CVE-2017-7819.html" type="cve" id="CVE-2017-7819" title="CVE-2017-7819" />
                <reference href="https://www.redhat.com/security/data/cve/CVE-2017-7823.html" type="cve" id="CVE-2017-7823" title="CVE-2017-7823" />
                <reference href="https://www.redhat.com/security/data/cve/CVE-2017-7824.html" type="cve" id="CVE-2017-7824" title="CVE-2017-7824" />
                <reference href="https://access.redhat.com/security/updates/classification/#critical" type="other" id="classification" title="critical" />
                <reference href="https://www.mozilla.org/en-US/security/advisories/mfsa2017-22/" type="other" id="ref_0" title="other_reference_0" />
            </references>
            <pkglist>
                <collection short="" >
                    <name>rhel-7-server-rpms__7_DOT_4__x86_64</name>
                    <package src="firefox-52.4.0-1.el7_4.src.rpm" name="firefox" epoch="0" version="52.4.0" release="1.el7_4" arch="x86_64" >
                        <filename>firefox-52.4.0-1.el7_4.x86_64.rpm</filename>
                        <sum type="sha256" >7b81b37bf969534bee0152bc13db56ae410eee06120a78d8da261c10c73c0514</sum>
                    </package>
                </collection>
            </pkglist>
        </update>
        <update status="final" from="[email protected]" version="2" type="bugfix" >
            <id>RHBA-2016:2423</id>
            <issued date="2016-11-03 06:09:21 UTC" />
            <title>oscap-anaconda-addon bug fix update</title>
            <release>0</release>
            <rights>Copyright 2016 Red Hat Inc</rights>
            <severity>None</severity>
            <pushcount>2</pushcount>
            <updated date="2016-11-03 06:10:44 UTC" />
            <references>
                <reference href="https://access.redhat.com/errata/RHBA-2016:2423" type="self" id="RHBA-2016:2423" title="RHBA-2016:2423" />
                <reference href="https://bugzilla.redhat.com/show_bug.cgi?id=1269211" type="bugzilla" id="1269211" title="could move security section down to bottom since it's not as important as network spoke" />
            </references>
            <pkglist>
                <collection short="" >
                    <name>rhel-7-server-rpms__7_DOT_4__x86_64</name>
                    <package src="oscap-anaconda-addon-0.7-12.el7.src.rpm" name="oscap-anaconda-addon" epoch="0" version="0.7" release="12.el7" arch="noarch" >
                        <filename>oscap-anaconda-addon-0.7-12.el7.noarch.rpm</filename>
                        <sum type="sha256" >507fbf46ddaed0bb4087d3ef2b31db235473f3be36aaa9ed7df43279ed7e2f07</sum>
                    </package>
                </collection>
            </pkglist>
        </update>
stovfl

问题:如何在 XML 解析过程中检查条件


你在做什么,不是解析,因为这一行已经完成了解析:

tree = ET.parse(file_name, parser=parser)

你不需要通过parser=XMLParser,因为这是标准的解析器。
阅读参考:xml.etree.ElementTree.parse

您的示例代码循环了ElementTree 四次

for ele in tree.findall('update'):
    cveList = [
        ele.find('references/reference').get('id') if ele.find('references/reference').get('type') == 'cve' else None
        for cve in ele.find('references/reference')]

Every .find..., 将循环直到找到请求的元素或直到结束。
你应该避免这样的嵌套编码!

您可以reference通过一个循环获取所有元素,例如:

import xml.etree.ElementTree as ET

file_name = "test/updateinfo.xml"
tree = ET.parse(file_name)

cveList = []
for reference in tree.findall('update/references/reference'):
    if reference.attrib.get('type') == 'cve':
        cveList.append(reference.attrib.get('id'))

print(cveList)

输出

['CVE-2017-7793', 'CVE-2017-7810', 'CVE-2017-7814', 'CVE-2017-7818', 'CVE-2017-7819', 'CVE-2017-7823', 'CVE-2017-7824']

评论:每个更新项的 cveList,而不是获取一个列表中的所有项。我想在每次更新中迭代并获取其他属性

# Findall 'update' Elements in tree
for update in tree.findall('update'):
    # Findall 'references/reference' in update
    for reference in update.findall('references/reference'):
        if reference.attrib.get('type') == 'cve':
            # Find Element with tag <title> in update
            title = update.find('title').text
            # Append a Dict with keys 'title' and 'id'
            cveList.append({'title': title, 'id': reference.get('id')})

输出

[{'id': 'CVE-2017-7793', 'title': 'Critical: firefox security update'}, {'id': 'CVE-2017-7810', 'title': 'Critical: firefox security update'}, {'id': 'CVE-2017-7814', 'title': 'Critical: firefox security update'}, {'id': 'CVE-2017-7818', 'title': 'Critical: firefox security update'}, {'id': 'CVE-2017-7819', 'title': 'Critical: firefox security update'}, {'id': 'CVE-2017-7823', 'title': 'Critical: firefox security update'}, {'id': 'CVE-2017-7824', 'title': 'Critical: firefox security update'}]

用 Python 测试:2.7.9

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Python ElementTree解析XML

来自分类Dev

使用ElementTree的递归XML解析python

来自分类Dev

使用ElementTree解析XML

来自分类Dev

使用elementtree解析xml网站

来自分类Dev

使用elementtree解析xml站点

来自分类Dev

使用ElementTree在Python中解析XML-findall

来自分类Dev

使用python ElementTree解析XML:ParseError:标记不匹配

来自分类Dev

Python-使用ElementTree解析具有重复标签的XML

来自分类Dev

使用ElementTree在Python中解析XML-findall

来自分类Dev

在Python中使用ElementTree进行XML解析时检查子代的存在

来自分类Dev

使用ElementTree进行Python XML解析:如何查找具有相同名称的元素的值?

来自分类Dev

XML解析Python ElementTree-嵌套循环

来自分类Dev

Python - ElementTree XML API(未按预期解析)

来自分类Dev

使用ElementTree Python获取检索XML属性

来自分类Dev

使用python的ElementTree处理xml子对象

来自分类Dev

在Python中使用ElementTree从XML提取数据

来自分类Dev

使用Python ElementTree从XML中提取值

来自分类Dev

使用xml子对象,Python中的ElementTree

来自分类Dev

使用ElementTree顺序解析某些XML标签

来自分类Dev

使用xml.etree.ElementTree的Python xml解析不起作用

来自分类Dev

Python xml.ElementTree - 在变量中返回解析后的 xml 以供稍后使用的函数

来自分类Dev

如何使用ElementTree递归遍历Python中的XML标签?

来自分类Dev

如何使用ElementTree在python中制作xml树的副本?

来自分类Dev

如何使用Python elementTree提取xml数据中的特定元素

来自分类Dev

使用xml.etree.ElementTree(python)解析XML时,如何区分常规空格和转义空格(&#32;)

来自分类Dev

如何使用ElementTree正确解析utf-8 xml?

来自分类Dev

如何使用ElementTree解析XML文件并检索子元素

来自分类Dev

python(xml.etree.ElementTree)中的XML解析

来自分类Dev

ElementTree:解析XML的曾孙

Related 相关文章

  1. 1

    使用Python ElementTree解析XML

  2. 2

    使用ElementTree的递归XML解析python

  3. 3

    使用ElementTree解析XML

  4. 4

    使用elementtree解析xml网站

  5. 5

    使用elementtree解析xml站点

  6. 6

    使用ElementTree在Python中解析XML-findall

  7. 7

    使用python ElementTree解析XML:ParseError:标记不匹配

  8. 8

    Python-使用ElementTree解析具有重复标签的XML

  9. 9

    使用ElementTree在Python中解析XML-findall

  10. 10

    在Python中使用ElementTree进行XML解析时检查子代的存在

  11. 11

    使用ElementTree进行Python XML解析:如何查找具有相同名称的元素的值?

  12. 12

    XML解析Python ElementTree-嵌套循环

  13. 13

    Python - ElementTree XML API(未按预期解析)

  14. 14

    使用ElementTree Python获取检索XML属性

  15. 15

    使用python的ElementTree处理xml子对象

  16. 16

    在Python中使用ElementTree从XML提取数据

  17. 17

    使用Python ElementTree从XML中提取值

  18. 18

    使用xml子对象,Python中的ElementTree

  19. 19

    使用ElementTree顺序解析某些XML标签

  20. 20

    使用xml.etree.ElementTree的Python xml解析不起作用

  21. 21

    Python xml.ElementTree - 在变量中返回解析后的 xml 以供稍后使用的函数

  22. 22

    如何使用ElementTree递归遍历Python中的XML标签?

  23. 23

    如何使用ElementTree在python中制作xml树的副本?

  24. 24

    如何使用Python elementTree提取xml数据中的特定元素

  25. 25

    使用xml.etree.ElementTree(python)解析XML时,如何区分常规空格和转义空格(&#32;)

  26. 26

    如何使用ElementTree正确解析utf-8 xml?

  27. 27

    如何使用ElementTree解析XML文件并检索子元素

  28. 28

    python(xml.etree.ElementTree)中的XML解析

  29. 29

    ElementTree:解析XML的曾孙

热门标签

归档