XML Declaration standalone="yes" lxml

17,162

Solution 1

You can pass standalone keyword argument to tostring():

etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8', standalone=True)

Solution 2

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

Solution 3

If you want to show the standalone='no' argument in your XML header, you have to set it to False instead of 'no'. Just like this:

etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8', standalone=False)

If not, standalone will be set to 'yes' by default.

Share:
17,162
speedyrazor
Author by

speedyrazor

Updated on June 19, 2022

Comments

  • speedyrazor
    speedyrazor almost 2 years

    I have an xml I am parsing, making some changes and saving out to a new file. It has the declaration <?xml version="1.0" encoding="utf-8" standalone="yes"?> which I would like to keep. When I am saving out my new file I am loosing the standalone="yes" bit. How can I keep it in? Here is my code:

    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'))
    
  • speedyrazor
    speedyrazor almost 11 years
    Sorry, your answer worked like a charm, I just thought @alecxe answer was easier to implement for me, thanks anyway for your answer, it's good to have options.
  • falsetru
    falsetru almost 11 years
    @user2446702, Okay, I see.
  • Arnold Roa
    Arnold Roa about 8 years
    TypeError: tostring() got an unexpected keyword argument 'xml_declaration'
  • alecxe
    alecxe about 8 years
    @ArnoldRoa are you using lxml.etree?
  • juanitogan
    juanitogan over 4 years
    No, not okay. This is the answer that doesn't invent data.
  • falsetru
    falsetru over 4 years
    @juanitogan, Sorry, I don't get what you mean. Could you give more information?
  • juanitogan
    juanitogan over 4 years
    @falsetru - What I mean is that it is not okay that the OP chose the other answer as correct. Yours is more correct because it doesn't override input values with hard-coded values. [This you know -- I'm just talking to everybody else.] The other answer is easier to implement ONLY because it requires fewer keystrokes on a single line, and is just lazy programming more prone to contribute to issues at some point.