如何访问多个xmlns
XML 树根元素的声明?例如:
import xml.etree.cElementTree as ET
data = """<root
xmlns:one="http://www.first.uri/here/"
xmlns:two="http://www.second.uri/here/">
...all other child elements here...
</root>"""
tree = ET.fromstring(data)
# I don't know what to do here afterwards
我想要一本与此类似的字典,或者至少是某种格式,以便更容易获取 URI 和匹配的标签
{'one':"http://www.first.uri/here/", 'two':"http://www.second.uri/here/"}
我不知道如何做到这一点xml.etree
,但与lxml.etree http://codespeak.net/lxml/你可以这样做:
import lxml.etree as le
data = """<root
xmlns:one="http://www.first.uri/here/"
xmlns:two="http://www.second.uri/here/">
...all other child elements here...
</root>"""
tree = le.XML(data)
print(tree.nsmap)
# {'two': 'http://www.second.uri/here/', 'one': 'http://www.first.uri/here/'}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)