使用Python中的ElementTree发出命名空间规范

Pau*_*han 29 python xml elementtree

我正在尝试使用包含XML声明和命名空间的元素树发出XML文件.这是我的示例代码:

from xml.etree import ElementTree as ET
ET.register_namespace('com',"http://www.company.com") #some name

# build a tree structure
root = ET.Element("STUFF")
body = ET.SubElement(root, "MORE_STUFF")
body.text = "STUFF EVERYWHERE!"

# wrap it in an ElementTree instance, and save as XML
tree = ET.ElementTree(root)

tree.write("page.xml",
           xml_declaration=True,
           method="xml" )
Run Code Online (Sandbox Code Playgroud)

但是,<?xml标签既没有出现也没有任何名称空间/前缀信息.我在这里有点困惑.

Mar*_*nen 36

虽然文档说不然,但我只能<?xml>通过指定xml_declaration和编码来获得声明.

您必须在已注册的命名空间中声明节点以获取文件中节点上的命名空间.这是您的代码的固定版本:

from xml.etree import ElementTree as ET
ET.register_namespace('com',"http://www.company.com") #some name

# build a tree structure
root = ET.Element("{http://www.company.com}STUFF")
body = ET.SubElement(root, "{http://www.company.com}MORE_STUFF")
body.text = "STUFF EVERYWHERE!"

# wrap it in an ElementTree instance, and save as XML
tree = ET.ElementTree(root)

tree.write("page.xml",
           xml_declaration=True,encoding='utf-8',
           method="xml")
Run Code Online (Sandbox Code Playgroud)

输出(page.xml)

<?xml version='1.0' encoding='utf-8'?><com:STUFF xmlns:com="http://www.company.com"><com:MORE_STUFF>STUFF EVERYWHERE!</com:MORE_STUFF></com:STUFF>
Run Code Online (Sandbox Code Playgroud)

ElementTree也不漂亮.这是漂亮的打印输出:

<?xml version='1.0' encoding='utf-8'?>
<com:STUFF xmlns:com="http://www.company.com">
    <com:MORE_STUFF>STUFF EVERYWHERE!</com:MORE_STUFF>
</com:STUFF>
Run Code Online (Sandbox Code Playgroud)

您还可以声明默认命名空间,而不需要注册一个:

from xml.etree import ElementTree as ET

# build a tree structure
root = ET.Element("{http://www.company.com}STUFF")
body = ET.SubElement(root, "{http://www.company.com}MORE_STUFF")
body.text = "STUFF EVERYWHERE!"

# wrap it in an ElementTree instance, and save as XML
tree = ET.ElementTree(root)

tree.write("page.xml",
           xml_declaration=True,encoding='utf-8',
           method="xml",default_namespace='http://www.company.com')
Run Code Online (Sandbox Code Playgroud)

输出(漂亮的打印间距是我的)

<?xml version='1.0' encoding='utf-8'?>
<STUFF xmlns="http://www.company.com">
    <MORE_STUFF>STUFF EVERYWHERE!</MORE_STUFF>
</STUFF>
Run Code Online (Sandbox Code Playgroud)


Phi*_*ham 8

我从来没有能够以<?xml编程方式从元素树库中获取标记,所以我建议你尝试这样的东西.

from xml.etree import ElementTree as ET
root = ET.Element("STUFF")
root.set('com','http://www.company.com')
body = ET.SubElement(root, "MORE_STUFF")
body.text = "STUFF EVERYWHERE!"

f = open('page.xml', 'w')
f.write('<?xml version="1.0" encoding="UTF-8"?>' + ET.tostring(root))
f.close()
Run Code Online (Sandbox Code Playgroud)

非std lib python ElementTree实现可能有不同的方式来指定命名空间,所以如果你决定转移到lxml,你声明它们的方式将是不同的.