Search code examples
pythonxmllxmlcxml

how to write the opening of an xml doc in lxml?


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.


Solution

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