使用ElementTree或cElementTree时,名称空间复杂化

时间:2012-11-15 10:52:36

标签: python xml elementtree celementtree

  

可能重复:
  python: xml.etree.ElementTree, removing “namespaces”

我有一个示例xml文档

<?xml version="1.0" encoding="UTF-8"?>
<school xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.school.org">
<name>John</name>
<name>Ferus</name>
<name>Greg</name>
</school>

我有以下python代码来查找名称,

import xml.etree.ElementTree as ElementTree

with open('sample.xml', 'rt') as f:
    tree = ElementTree.parse(f)
root = tree.getroot()
for name in root.findall("{http://www.school.org}name"):
    print name.text

每次搜索标签时提供命名空间都很笨拙。还有更好的方法吗?

0 个答案:

没有答案