How to write XML declaration using xml.etree.ElementTree

后端 未结 11 942
长情又很酷
长情又很酷 2020-11-30 05:11

I am generating an XML document in Python using an ElementTree, but the tostring function doesn\'t include an XML declaration when converting to plaintext.

相关标签:
11条回答
  • 2020-11-30 06:03

    I would use lxml (see http://lxml.de/api.html).

    Then you can:

    from lxml import etree
    document = etree.Element('outer')
    node = etree.SubElement(document, 'inner')
    print(etree.tostring(document, xml_declaration=True))
    
    0 讨论(0)
  • 2020-11-30 06:05

    I am surprised to find that there doesn't seem to be a way with ElementTree.tostring(). You can however use ElementTree.ElementTree.write() to write your XML document to a fake file:

    from io import BytesIO
    from xml.etree import ElementTree as ET
    
    document = ET.Element('outer')
    node = ET.SubElement(document, 'inner')
    et = ET.ElementTree(document)
    
    f = BytesIO()
    et.write(f, encoding='utf-8', xml_declaration=True) 
    print(f.getvalue())  # your XML file, encoded as UTF-8
    

    See this question. Even then, I don't think you can get your 'standalone' attribute without writing prepending it yourself.

    0 讨论(0)
  • 2020-11-30 06:06

    This works if you just want to print. Getting an error when I try to send it to a file...

    import xml.dom.minidom as minidom
    import xml.etree.ElementTree as ET
    from xml.etree.ElementTree import Element, SubElement, Comment, tostring
    
    def prettify(elem):
        rough_string = ET.tostring(elem, 'utf-8')
        reparsed = minidom.parseString(rough_string)
        return reparsed.toprettyxml(indent="  ")
    
    0 讨论(0)
  • 2020-11-30 06:08

    Easy

    Sample for both Python 2 and 3 (encoding parameter must be utf8):

    import xml.etree.ElementTree as ElementTree
    
    tree = ElementTree.ElementTree(ElementTree.fromstring('<xml><test>123</test></xml>'))
    root = tree.getroot()
    print(ElementTree.tostring(root, encoding='utf8', method='xml'))
    

    From Python 3.8 there is xml_declaration parameter for that stuff:

    New in version 3.8: The xml_declaration and default_namespace parameters.

    xml.etree.ElementTree.tostring(element, encoding="us-ascii", method="xml", *, xml_declaration=None, default_namespace=None, short_empty_elements=True) Generates a string representation of an XML element, including all subelements. element is an Element instance. encoding 1 is the output encoding (default is US-ASCII). Use encoding="unicode" to generate a Unicode string (otherwise, a bytestring is generated). method is either "xml", "html" or "text" (default is "xml"). xml_declaration, default_namespace and short_empty_elements has the same meaning as in ElementTree.write(). Returns an (optionally) encoded string containing the XML data.

    Sample for Python 3.8 and higher:

    import xml.etree.ElementTree as ElementTree
    
    tree = ElementTree.ElementTree(ElementTree.fromstring('<xml><test>123</test></xml>'))
    root = tree.getroot()
    print(ElementTree.tostring(root, encoding='unicode', method='xml', xml_declaration=True))
    
    0 讨论(0)
  • 2020-11-30 06:09

    Another pretty simple option is to concatenate the desired header to the string of xml like this:

    xml = (bytes('<?xml version="1.0" encoding="UTF-8"?>\n', encoding='utf-8') + ET.tostring(root))
    xml = xml.decode('utf-8')
    with open('invoice.xml', 'w+') as f:
        f.write(xml)
    
    0 讨论(0)
提交回复
热议问题