XML和Python:获取根元素中声明的名称空间
如何访问XML树根元素处的多个XML和Python:获取根元素中声明的名称空间,python,xml,xml-namespaces,Python,Xml,Xml Namespaces,如何访问XML树根元素处的多个xmlns声明?例如: 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... &l
xmlns
声明?例如:
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
我不确定如何使用
xml.etree
实现这一点,但您可以使用:
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/'}
将lxml.etree导入为le
data=”“”
…此处的所有其他子元素。。。
"""
tree=le.XML(数据)
打印(tree.nsmap)
#{'2':'http://www.second.uri/here/'一':'http://www.first.uri/here/'}
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/'}