简体   繁体   English

Python:通过 iter() 在 ElementTree 中使用命名空间

[英]Python: Using namespaces in ElementTree with iter()

I am using xml.etree.ElementTree to parse some complex xml files.我正在使用 xml.etree.ElementTree 来解析一些复杂的 xml 文件。 Some of the xml files have several repeating tags nested in them.一些 xml 文件中有几个嵌套在其中的重复标签。

 <product:object> <product:parent> <product:parent> <product:parent> </product:parent> </product:parent> </product:parent> </product:object>

I am using .iter() to find the repeating tag in different layers.我正在使用 .iter() 来查找不同层中的重复标签。 Normally a second argument can be passed to .find() and .findall().通常可以将第二个参数传递给 .find() 和 .findall()。 However, for some reason .iter() doesn't have this option.但是,由于某种原因 .iter() 没有这个选项。

Am I missing something, or is there another way of properly doing this?我错过了什么,还是有另一种方法可以正确地做到这一点?

I know how to, and have build workarounds.我知道该怎么做,并且已经建立了解决方法。 eg - A definition that is reiterated and passes the parent element.例如 - 重复并传递父元素的定义。 - Manually mapping the namespaces - 手动映射命名空间

I am hoping there is a better way!?我希望有更好的方法!?

我发现使用 XPath 语法 .findall(.//product:parent, ns) 可以代替 .iter()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM