python:xml.etree.ElementTree,删除“命名空间"; [英] python: xml.etree.ElementTree, removing "namespaces"

查看:60
本文介绍了python:xml.etree.ElementTree,删除“命名空间";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢 ElementTree 解析 xml 的方式,尤其是 Xpath 功能.我有一个带有嵌套标签的应用程序的 xml 输出.

I like the way ElementTree parses xml, in particular the Xpath feature. I've an output in xml from an application with nested tags.

我想在不指定命名空间的情况下按名称访问此标签,这可能吗?例如:

I'd like to access this tags by name without specifying the namespace, is it possible? For example:

root.findall("/molpro/job")

代替:

root.findall("{http://www.molpro.net/schema/molpro2006}molpro/{http://www.molpro.net/schema/molpro2006}job")

推荐答案

至少使用 lxml2,可以在一定程度上减少这种开销:

At least with lxml2, it's possible to reduce this overhead somewhat:

root.findall("/n:molpro/n:job",
             namespaces=dict(n="http://www.molpro.net/schema/molpro2006"))

这篇关于python:xml.etree.ElementTree,删除“命名空间";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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