Premysl Vorac
Premysl Vorac

Reputation: 473

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, i.e.

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

Upvotes: 8

Views: 5580

Answers (2)

Fausto Alonso
Fausto Alonso

Reputation: 1306

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"?>'
            ))

Upvotes: 15

Premysl Vorac
Premysl Vorac

Reputation: 473

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?

Upvotes: 2

Related Questions