我使用ElementTree
在Python中生成XML文档,但tostring
函数在转换为纯文本时不包含XML declaration。如何使用xml.etree.ElementTree编写XML声明
from xml.etree.ElementTree import Element, tostring
document = Element('outer')
node = SubElement(document, 'inner')
node.NewValue = 1
print tostring(document) # Outputs "<outer><inner /></outer>"
我需要我的字符串包括以下XML声明:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
然而,似乎没有被这样做的任何记录的方式。
是否有合适的方法来呈现ElementTree
中的XML声明?
为什么在这里定义“节点”变量? – 2015-06-11 11:01:08
感谢这条线et.write(f,encoding ='utf-8',xml_declaration = True)保存了我的日子 – 2016-10-20 09:16:49