如何使用xml.etree.ElementT写XML声明

2024-04-26 09:04:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python中使用^{}生成XML文档,但是tostring函数在转换为纯文本时不包含XML declaration

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声明的正确方法?


Tags: 方法函数文档文本node声明xmlelement
3条回答

我很惊讶地发现,似乎没有办法处理ElementTree.tostring()。但是,您可以使用ElementTree.ElementTree.write()将XML文档写入假文件:

from io import BytesIO
from xml.etree import ElementTree as ET

document = ET.Element('outer')
node = ET.SubElement(document, 'inner')
et = ET.ElementTree(document)

f = BytesIO()
et.write(f, encoding='utf-8', xml_declaration=True) 
print(f.getvalue())  # your XML file, encoded as UTF-8

this question。即使这样,我也不认为不自己编写预处理就可以获得“standalone”属性。

If you include the ^{}, you will get an XML header

xml.etree.ElementTree.tostring writes a XML encoding declaration with encoding='utf8'

示例Python代码(适用于Python 2和3):

import xml.etree.ElementTree as ElementTree

tree = ElementTree.ElementTree(
    ElementTree.fromstring('<xml><test>123</test></xml>')
)
root = tree.getroot()

print('without:')
print(ElementTree.tostring(root, method='xml'))
print('')
print('with:')
print(ElementTree.tostring(root, encoding='utf8', method='xml'))

Python 2输出:

$ python2 example.py
without:
<xml><test>123</test></xml>

with:
<?xml version='1.0' encoding='utf8'?>
<xml><test>123</test></xml>

对于Python 3,您将注意到the ^{} prefix表示返回字节文本(就像Python 2一样):

$ python3 example.py
without:
b'<xml><test>123</test></xml>'

with:
b"<?xml version='1.0' encoding='utf8'?>\n<xml><test>123</test></xml>"

我会使用lxml(参见http://lxml.de/api.html)。

然后你可以:

from lxml import etree
document = etree.Element('outer')
node = etree.SubElement(document, 'inner')
print(etree.tostring(document, xml_declaration=True))

相关问题 更多 >