Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing lxml.etree with double quotes header attributes

Tags:

python

lxml

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"?>
....
like image 941
Premysl Vorac Avatar asked Oct 04 '17 13:10

Premysl Vorac


People also ask

Is XML and lxml are same?

lxml is a reference to the XML toolkit in a pythonic way which is internally being bound with two specific libraries of C language, libxml2, and libxslt. lxml is unique in a way that it combines the speed and XML feature completeness of these libraries with the simplicity of a native Python API.


1 Answers

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"?>'
            ))
like image 108
Fausto Alonso Avatar answered Sep 23 '22 13:09

Fausto Alonso