我有一个xml我正在解析,进行一些更改并保存到一个新文件。它有我想保留的声明<?xml version="1.0" encoding="utf-8" standalone="yes"?>
。当我保存我的新文件时,我丢失了standalone="yes"
位。我该如何保管?
这是我的代码:
templateXml = """<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<package>
<provider>Some Data</provider>
<studio_display_name>Some Other Data</studio_display_name>
</package>"""
from lxml import etree
tree = etree.fromstring(templateXml)
xmlFileOut = '/Users/User1/Desktop/Python/Done.xml'
with open(xmlFileOut, "w") as f:
f.write(etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8'))
答案 0 :(得分:15)
您可以将standalone
关键字参数传递给tostring()
:
etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8', standalone="yes")
答案 1 :(得分:8)
使用tree.docinfo.standalone指定standalone
。
请尝试以下操作:
from lxml import etree
tree = etree.fromstring(templateXml).getroottree() # NOTE: .getroottree()
xmlFileOut = '/Users/User1/Desktop/Python/Done.xml'
with open(xmlFileOut, "w") as f:
f.write(etree.tostring(tree, pretty_print=True, xml_declaration=True,
encoding=tree.docinfo.encoding,
standalone=tree.docinfo.standalone))
答案 2 :(得分:2)
如果要在XML标头中显示standalone='no'
参数,则必须将其设置为False
而不是“否”。就像这样:
etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8', standalone=False)
如果没有,默认情况下,standalone将设置为“是”。
答案 3 :(得分:0)
etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8')
如果您正在使用lxml,则会添加声明,但我注意到它们的声明使用半引号而不是完整引号。
您还可以通过将输出与您需要的静态字符串连接来获得所需的确切声明:
xml = etree.tostring(tree, pretty_print = True, encoding='UTF-8')
xml = '<?xml version=\"1.0\" encoding=\"utf-8\"?>\n' + xml
答案 4 :(得分:0)
如果您要在所有通行证standalone
而非None
或True
处停用输出False
。听起来合乎逻辑,但花了一些时间才真正找到并测试它。
etree.tostring(tree, xml_declaration = True, encoding='utf-8', standalone=None)
或使用上下文管理器和流etree.xmlfile
序列化:
with etree.xmlfile(open('/tmp/test.xml'), encoding='utf-8') as xf:
xf.write_declaration(standalone=None)
with xf.element('html'):
with xf.element('body'):
element = etree.Element('p')
element.text = 'This is paragraph'
xf.write(element)