我正在使用Python在Python中生成XML文档ElementTree
,但该tostring
函数在转换为纯文本时不包含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>"
Run Code Online (Sandbox Code Playgroud)
我需要我的字符串包含以下XML声明:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
Run Code Online (Sandbox Code Playgroud)
但是,似乎没有任何记录的方法可以做到这一点.
是否有适当的方法来呈现XML声明ElementTree
?