python-如何使用xml.etree.Element编写XML声明
我正在使用ElementTree在Python中生成XML文档,但是ElementTree函数在转换为纯文本时不包含XML声明。
from xml.etree.ElementTree import Element, tostring
document = Element('outer')
node = SubElement(document, 'inner')
node.NewValue = 1
print tostring(document) # Outputs ""
我需要包含以下XML声明的字符串:
但是,似乎没有任何记录的方式来执行此操作。
在ElementTree中是否存在用于呈现XML声明的适当方法?
9个解决方案
85 votes
我很惊讶地发现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
看到这个问题。 即使那样,我认为如果不自己编写,就无法获得“独立”属性。
wrgrs answered 2020-01-23T12:28:44Z
22 votes
我将使用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))
glormph answered 2020-01-23T12:29:08Z
14 votes
如果包含encoding='utf8',您将获得XML标头:
xml.etree.ElementTree.tostring使用encoding ='utf8'编写XML编码声明
示例Python代码(适用于Python 2和3):
import xml.etree.ElementTree as ElementTree
tree = ElementTree.ElementTree(
ElementTree.fromstring('123')
)
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:
123
with:
123
使用Python 3时,您会注意到b前缀,该前缀指示返回字节文字(就像Python 2一样):
$ python3 example.py
without:
b'123'
with:
b"<?xml version='1.0' encoding='utf8'?>\n123"
Alexander O'Mara answered 2020-01-23T12:29:46Z
3 votes
我最近遇到此问题,在对代码进行了一些挖掘之后,我发现以下代码片段是函数的定义encoding
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或2715306863817655655以外的encoding参数。 UTF-8
alijandro answered 2020-01-23T12:30:10Z
2 votes
包含ElementTree包用法的最小工作示例:
import xml.etree.ElementTree as ET
document = ET.Element('outer')
node = ET.SubElement(document, 'inner')
node.text = '1'
res = ET.tostring(document, encoding='utf8', method='xml').decode()
print(res)
输出为:
1
Andriy answered 2020-01-23T12:30:34Z
0 votes
我会使用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))
Alessandro answered 2020-01-23T12:30:54Z
0 votes
如果您只想打印,则此方法有效。 尝试将其发送到文件时出现错误...
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=" ")
Rebecca Fallon answered 2020-01-23T12:31:14Z
0 votes
在声明中包括“独立”
我在文档中找不到添加standalone自变量的任何替代方法,因此我改编了ET.tosting函数以将其作为自变量。
from xml.etree import ElementTree as ET
# Sample
document = ET.Element('outer')
node = ET.SubElement(document, 'inner')
et = ET.ElementTree(document)
# Function that you need
def tostring(element, declaration, encoding=None, method=None,):
class dummy:
pass
data = []
data.append(declaration+"\n")
file = dummy()
file.write = data.append
ET.ElementTree(element).write(file, encoding, method=method)
return "".join(data)
# Working example
xdec = """<?xml version="1.0" encoding="UTF-8" standalone="no" ?>"""
xml = tostring(document, encoding='utf-8', declaration=xdec)
G M answered 2020-01-23T12:31:39Z
0 votes
另一个非常简单的选项是将所需的标头连接到xml的字符串中,如下所示:
xml = (bytes('<?xml version="1.0" encoding="UTF-8"?>\n', encoding='utf-8') + ET.tostring(root))
xml = xml.decode('utf-8')
with open('invoice.xml', 'w+') as f:
f.write(xml)
Novak answered 2020-01-23T12:31:59Z