如何使用xml.etree.ElementTree编写XML声明
我使用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声明?
我很惊讶地发现,似乎没有要与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。即使那样,我也不认为你可以在没有自己预先写好的情况下得到你的“独立”属性。
我会使用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))
我最近遇到此问题,代码的一些挖后,我发现下面的代码片段的功能ElementTree.write
def write(self, file, encoding="us-ascii"):
assert self._root is not None
if not hasattr(file, "write"):
file = open(file, "wb")
if not encoding:
encoding = "us-ascii"
elif encoding != "utf-8" and encoding != "us-ascii":
file.write("<?xml version='1.0' encoding='%s'?>\n" %
encoding)
self._write(file, self._root, encoding, {})
定义因此,答案是,如果需要XML头写入到文件,设置除utf-8
或us-ascii
之外的其他参数encoding
,例如UTF-8
这虽然很脆弱,但它似乎不起作用(编码可能更低 - 在此之前)。此外,'ElementTree.ElementTree.write()'被记录为具有'xml_declaration'参数(请参阅接受的答案)。但是'ElementTree。tostring()'没有这个参数,这是在原始问题中提出的方法。 – 2015-04-14 07:31:47
我会用ET:
try:
from lxml import etree
print("running with lxml.etree")
except ImportError:
try:
# Python 2.5
import xml.etree.cElementTree as etree
print("running with cElementTree on Python 2.5+")
except ImportError:
try:
# Python 2.5
import xml.etree.ElementTree as etree
print("running with ElementTree on Python 2.5+")
except ImportError:
try:
# normal cElementTree install
import cElementTree as etree
print("running with cElementTree")
except ImportError:
try:
# normal ElementTree install
import elementtree.ElementTree as etree
print("running with ElementTree")
except ImportError:
print("Failed to import ElementTree from any known place")
document = etree.Element('outer')
node = etree.SubElement(document, 'inner')
print(etree.tostring(document, encoding='UTF-8', xml_declaration=True))
这工作,如果你只想打印。得到一个错误,当我尝试把它发送到一个文件...
import xml.dom.minidom as minidom
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
def prettify(elem):
rough_string = ET.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
If you include the encoding='utf8'
, you will get an XML header:
xml.etree.ElementTree.tostring写了一个XML编码声明与编码= 'utf-8'
样品的Python 2代码:
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')
输出:
without:
<xml><test>123</test></xml>
with:
<?xml version='1.0' encoding='utf8'?>
<xml><test>123</test></xml>
在Python 3中,转义字符将在打印时显示在声明中。 '' – 2017-10-30 16:05:46
为什么在这里定义“节点”变量? – 2015-06-11 11:01:08
感谢这条线et.write(f,encoding ='utf-8',xml_declaration = True)保存了我的日子 – 2016-10-20 09:16:49