简体   繁体   中英

Writing lxml.etree with double quotes header attributes

I have created a basic xml tree using lxml tutorial :

from lxml import etree
root = etree.Element("root")
root.append( etree.Element("child1") )
child2 = etree.SubElement(root, "child2")
child3 = etree.SubElement(root, "child3")
print(etree.tostring(root, pretty_print=True, encoding="UTF-8", xml_declaration=True))

This produces the following:

<?xml version='1.0' encoding='UTF-8'?>
<root>
  <child1/>
  <child2/>
  <child3/>
</root>

My question is, how to produce xml file with double quoted file header, ie

<?xml version="1.0" encoding="UTF-8"?>
....

To add the header without concatenate manually you need to use the "doctype" parameter in tostring method as bellow:

        with open(output_file, 'wb') as o:
            o.write(etree.tostring(
                document_root, pretty_print=True,
                doctype='<?xml version="1.0" encoding="ISO-8859-1"?>'
            ))

The only solution I have right now is this function:

def wrTmp(treeObject, filepath):
    xml_str = ('<?xml version="1.0" encoding="UTF-8"?>' + '\n' +
               etree.tostring(treeObject, pretty_print=True, encoding="UTF-8",
                              xml_declaration=False))
    with open(filepath, 'wb') as xml_file:
        xml_file.write(xml_str)

The function concatenetes two strings. One with file header and newline and the second one with the rest of the xml tree.

Does anyone know a more "Pythonic" way to do this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM