使用find方法(xml.etree.ElementTree)后获取父元素 [英] Get parent element after using find method (xml.etree.ElementTree)

查看:917
本文介绍了使用find方法(xml.etree.ElementTree)后获取父元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个巨大的xml文件,并尝试从不同的元素中提取信息。

I am working with a huge xml-file and try to extract information from different elements.

import xml.etree.ElementTree as ET
tree = ET.parse('t.xml')
root = tree.getroot()

要查找元素,我使用find方法:

To find the elements I use the find method:

elm = root.find('.//Element[@elmid="1234"]')

我从中提取信息并在另外,我需要来自父元素的信息。但是 elm.find('..')仅返回 None ,如此处记录:
https://docs.python.org/3/library/xml.etree.elementtree。 html

From this I extract information and in addition I need information from the parent element. But elm.find('..') returns only None as documented here: https://docs.python.org/3/library/xml.etree.elementtree.html

现在我使用以下方法:

prt = root.find('.//Element[@elmid="1234"]/..')     
elm = prt.find('/Element[@elmid="1234"]')

这对我来说似乎有点不自然,但可以。

This looks a bit unnatural to me, but works.

您知道更好的方法吗?
您知道为什么只返回 None 吗?

Do you know a better way to do this? Do you know why only None is returned?

推荐答案

xml.etree API仅支持XPath的受限版本。 xml.etree docs 表示 XPath表达式状态:

The xml.etree API only supports a limited version of XPath. The xml.etree docs for the .. XPath expression state:


选择父元素。 如果路径尝试使
到达start元素的祖先(查找到的元素称为
的祖先),则不返回任何值。

直接获取父元素是 xml.etree不支持 API 。因此,我建议使用 lxml ,您可以在其中简单地使用< a href = http://lxml.de/api/lxml.etree._Element-class.html#getparent rel = noreferrer> getparent() 获取父元素:

Directly getting the parent element is not supported in the xml.etree API. I would therefore recommend to use lxml, where you can simply use getparent() to get the parent element:

elm = root.find('.//Element[@elmid="1234"]')
elm.getparent()

lxml 还具有完整的 XPath 1.0实现,因此 elem.xpath ('..')也可以。

lxml also has a full XPath 1.0 implementation, so elem.xpath('..') would work as well.

这篇关于使用find方法(xml.etree.ElementTree)后获取父元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆