Python:忽略 xml.etree.ElementTree 中的命名空间? [英] Python: ignoring namespaces in xml.etree.ElementTree?
本文介绍了Python:忽略 xml.etree.ElementTree 中的命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何告诉 ElementTree 忽略 XML 文件中的命名空间?
How can I tell ElementTree to ignore namespaces in an XML file?
例如,我更喜欢查询modelVersion
(如语句1)而不是{http://maven.apache.org/POM/4.0.0}modelVersion代码>(如语句 2 中所示).
For example, I would prefer to query modelVersion
(as in statement 1) rather than {http://maven.apache.org/POM/4.0.0}modelVersion
(as in statement 2).
pom="""
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
</project>
"""
from xml.etree import ElementTree
ElementTree.register_namespace("","http://maven.apache.org/POM/4.0.0")
root = ElementTree.fromstring(pom)
print 1,root.findall('modelVersion')
print 2,root.findall('{http://maven.apache.org/POM/4.0.0}modelVersion')
1 []
2 [<Element '{http://maven.apache.org/POM/4.0.0}modelVersion' at 0x1006bff10>]
推荐答案
这是不使用 shell 的等效解决方案.基本思路:
Here's the equivalent solution without using the shell. Basic idea:
- 将
<项目垃圾...>
翻译成 - 执行干净"处理而无需担心命名空间
- 将
翻译回
- translate
<project junk...>
to<project>
- perform "clean" processing without worrying about the namespace
- translate
<project>
back to<project junk...>
使用新代码:
pom="""
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
</project>
"""
short_project="""<project>"""
long_project="""<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">"""
import re,sys
from xml.etree import ElementTree
# eliminate namespace specs
pom=re.compile('<project [^>]*>').sub(short_project,pom)
root = ElementTree.fromstring(pom)
ElementTree.dump(root)
print 1,root.findall('modelVersion')
print 2,root.findall('{http://maven.apache.org/POM/4.0.0}modelVersion')
mv=root.findall('modelVersion')
# restore the namespace specs
pom=ElementTree.tostring(root)
pom=re.compile(short_project).sub(long_project,pom)
这篇关于Python:忽略 xml.etree.ElementTree 中的命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文