Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.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';基于子标记中的属性将元素树保存到列表中;s LXML模块?_Python_Xml_Parsing_Xpath_Lxml - Fatal编程技术网

如何使用Python';基于子标记中的属性将元素树保存到列表中;s LXML模块?

如何使用Python';基于子标记中的属性将元素树保存到列表中;s LXML模块?,python,xml,parsing,xpath,lxml,Python,Xml,Parsing,Xpath,Lxml,我需要解析一个xml文档。我正在使用Python3.8和lxml模块 XML包含具有其他子元素标记的标题,如下面的XML。我只需要找到“更改”事件,并将“标题”保留在列表中。我想保存该标题的所有标记,以便提取所需的数据 以下是我的XML示例: ''' <root> <Title ref="111111"> <Events> <Event type="change"/&

我需要解析一个xml文档。我正在使用Python3.8和lxml模块

XML包含具有其他子元素标记的标题,如下面的XML。我只需要找到“更改”事件,并将“标题”保留在列表中。我想保存该标题的所有标记,以便提取所需的数据

以下是我的XML示例:

'''
<root>
    <Title ref="111111">
        <Events>
            <Event type="change"/>
        </Events>
        <tag1>John</tag1>
        <tag2>A.</tag2>
        <tag3>Smith</tag3>
    </Title>
        <Title ref="222222">
        <Events>
            <Event type="cancel"/>
        </Events>
        <tag1>Bob</tag1>
        <tag2>B.</tag2>
        <tag3>Hope</tag3>
    </Title>
        <Title ref="333333">
        <Events>
            <Event type="change"/>
        </Events>
        <tag1>Julie</tag1>
        <tag2>A.</tag2>
        <tag3>Moore</tag3>
    </Title>
        <Title ref="444444">
        <Events>
            <Event type="cancel"/>
        </Events>
        <tag1>First</tag1>
        <tag2>M</tag2>
        <tag3>Last</tag3>
    </Title>
</root>
'''
“”
约翰
A.
史密斯
上下快速移动
B
希望
朱莉
A.
摩尔
弗斯特
M
最后
'''

我尝试过使用findall()函数,但它似乎只保留“Event”标记,而不是“Title”标记及其所有子项。我在使用xpath时也会得到同样的结果。

如果
txt
是问题中的XML片段,那么您可以这样做来提取包含
标记:

from lxml import etree, html

root = etree.fromstring(txt)

for title in root.xpath('.//Title[.//Event[@type="change"]]'):
    print(html.tostring(title).decode('utf-8'))
    print('-' * 80)
印刷品:

<Title ref="111111">
        <Events>
            <Event type="change"></Event>
        </Events>
        <tag1>John</tag1>
        <tag2>A.</tag2>
        <tag3>Smith</tag3>
    </Title>
        
--------------------------------------------------------------------------------
<Title ref="333333">
        <Events>
            <Event type="change"></Event>
        </Events>
        <tag1>Julie</tag1>
        <tag2>A.</tag2>
        <tag3>Moore</tag3>
    </Title>
        
--------------------------------------------------------------------------------

约翰
A.
史密斯
--------------------------------------------------------------------------------
朱莉
A.
摩尔
--------------------------------------------------------------------------------

如果
txt
是问题中的XML片段,则可以执行此操作以提取包含
标记:

from lxml import etree, html

root = etree.fromstring(txt)

for title in root.xpath('.//Title[.//Event[@type="change"]]'):
    print(html.tostring(title).decode('utf-8'))
    print('-' * 80)
印刷品:

<Title ref="111111">
        <Events>
            <Event type="change"></Event>
        </Events>
        <tag1>John</tag1>
        <tag2>A.</tag2>
        <tag3>Smith</tag3>
    </Title>
        
--------------------------------------------------------------------------------
<Title ref="333333">
        <Events>
            <Event type="change"></Event>
        </Events>
        <tag1>Julie</tag1>
        <tag2>A.</tag2>
        <tag3>Moore</tag3>
    </Title>
        
--------------------------------------------------------------------------------

约翰
A.
史密斯
--------------------------------------------------------------------------------
朱莉
A.
摩尔
--------------------------------------------------------------------------------

真管用!谢谢Andrej简单快速的回答。Xpath对我来说一直是一个问题。这很有效!谢谢Andrej简单快速的回答。Xpath对我来说一直是个问题。