简体   繁体   中英

Getting the XML without the <?xml …?> from minidom

I've used minidom to create an XML and it comes out correctly but I need it to be returned without the <?xml version="1.0" encoding="utf-8"?> at the beginning. Is there a way to get the XML without the <?xml?> tag?

You can try slicing the string at the first instance of ?> :

xml = xml[xml.index('?>') + 2:]

I looked at the source code of the xml.dom.minidom package and I think this will do the job as well:

import xml.dom.minidom

def writexml(self, writer, indent="", addindent="", newl="", encoding = None):
  for node in self.childNodes:
    node.writexml(writer, indent, addindent, newl)

xml.dom.minidom.Document.writexml = writexml

del writexml

Personally i just slice off the first 22 Chars

xml_out = doc.toxml()
return xml_out[22:]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM