2

I'm using lxml to write out a cXML file, but I can't figure out how to get it to write out the opening <?xml version="1.0" encoding="UTF-8"?> along with the doctype following it. When I started this, I started straight in on the document itself, with the first Element being cXML timestamp="2015-02-01'T'12:00:00Z">' and so on. Now I realize I'm probably getting parsing errors due to there not being the opening tags and a doctype definition, but I have no idea how to get lxml how to write those out.

Bendustries
  • 71
  • 1
  • 3
  • 10

1 Answers1

3

You can pass them as arguments to the tostring() method. An example:

from lxml import etree

root = etree.Element('root')
etree.SubElement(root, 'child1')
etree.SubElement(root, 'child2')

print etree.tostring(root, encoding='UTF-8', xml_declaration=True, doctype='''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">''')

That yields:

<?xml version='1.0' encoding='UTF-8'?>                                                                           
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"                                                                                                                                                                                                                       
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">                                                                                                                                                                                                                   
<root><child1/><child2/></root>
Birei
  • 35,723
  • 2
  • 77
  • 82
  • Worked perfectly. To write it out to file, I used this: `treeStr = ET.tostring(topline, encoding="UTF-8", xml_declaration=True, doctype=''' ''') xmlFile = open(str(orderNumber) + ".xml", 'w') xmlFile.write(treeStr)` – Bendustries Feb 15 '15 at 05:49