XML Declaration standalone=“yes” lxml

前端 未结 5 1213
暗喜
暗喜 2020-12-20 13:01

I have an xml I am parsing, making some changes and saving out to a new file. It has the declaration

5条回答
  •  别那么骄傲
    2020-12-20 13:22

    Specify standalone using tree.docinfo.standalone.

    Try following:

    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))
    

提交回复
热议问题