Modify XML declaration with python

前端 未结 1 480
死守一世寂寞
死守一世寂寞 2021-01-26 00:10

I have an XML document for which I need to add a couple of things to the XML declaration using minidom. The declaration looks like this:



        
相关标签:
1条回答
  • 2021-01-26 00:40

    I'm not sure if this can be done with minidom. But you could try lxml.

    from lxml import etree
    
    tree = etree.parse("test.xml")
    string = etree.tostring(tree.getroot(), pretty_print = True, xml_declaration = True, standalone = False, encoding = "UTF-16")
    with open("test2.xml", "wb") as f:
        f.write(string)
    

    More or less taken from here.

    0 讨论(0)
提交回复
热议问题