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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4997848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 18:33:59  来源:igfitidea点击:

Emitting namespace specifications with ElementTree in Python

pythonxmlelementtree

提问by Paul Nathan

I am trying to emit an XML file with element-tree that contains an XML declaration and namespaces. Here is my sample code:

我正在尝试使用包含 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" )

However, neither the <?xmltag comes out nor any namespace/prefix information. I'm more than a little confused here.

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

采纳答案by Mark Tolonen

Although the docssay otherwise, I only was able to get an <?xml>declaration by specifying both the xml_declaration and the encoding.

尽管文档另有说明,但我只能<?xml>通过指定 xml_declaration 和编码来获得声明。

You have to declare nodes in the namespace you've registered to get the namespace on the nodes in the file. Here's a fixed version of your code:

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

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")

Output (page.xml)

输出 (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>

ElementTree doesn't pretty-print either. Here's pretty-printed output:

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>


You can also declare a default namespace and don't need to register one:

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

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')

Output (pretty-print spacing is mine)

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

<?xml version='1.0' encoding='utf-8'?>
<STUFF xmlns="http://www.company.com">
    <MORE_STUFF>STUFF EVERYWHERE!</MORE_STUFF>
</STUFF>

回答by Philip Southam

I've never been able to get the <?xmltag out of the element tree libraries programatically so I'd suggest you try something like this.

我从来没有能够以<?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()

Non std lib python ElementTree implementations may have different ways to specify namespaces, so if you decide to move to lxml, the way you declare those will be different.

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