How to write an XML file without header in Python?

后端 未结 7 1407
盖世英雄少女心
盖世英雄少女心 2021-02-06 01:28

when using Python\'s stock XML tools such as xml.dom.minidom for XML writing, a file would always start off like

<

7条回答
  •  耶瑟儿~
    2021-02-06 01:53

    If you want to use minidom and maintain 'prettiness', how about this as a quick/hacky fix:

    xml_without_declaration.py:

    import xml.dom.minidom as xml
    
    doc = xml.Document()
    
    declaration = doc.toxml()
    
    a = doc.createElement("A")
    doc.appendChild(a)
    b = doc.createElement("B")
    a.appendChild(b)
    
    xml = doc.toprettyxml()[len(declaration):]
    
    print xml
    

提交回复
热议问题