Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XML和Python:获取根元素中声明的名称空间_Python_Xml_Xml Namespaces - Fatal编程技术网

XML和Python:获取根元素中声明的名称空间

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

如何访问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...
          </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/'}