when using Python's stock XML tools such as xml.dom.minidom
for XML writing, a file would always start off like
<?xml version="1.0"?>
[...]
While this is perfectly legal XML code, and it's even recommended to use the header, I'd like to get rid of it as one of the programs I'm working with has problems here.
I can't seem to find the appropriate option in xml.dom.minidom
, so I wondered if there are other packages which do allow to neglect the header.
Cheers,
Nico
Purists may not like to hear this, but I have found using an XML parser to generate XML to be overkill. Just generate it directly as strings. This also lets you generate files larger than you can keep in memory, which you can't do with DOM. Reading XML is another story.
If you want to use minidom and maintain 'prettiness', how about this as a quick/hacky fix:
xml_without_declaration.py:
If you're set on using minidom, just scan back in the file and remove the first line after writing all the XML you need.
Unfortunately
minidom
does not give you the option to omit the XML Declaration.But you can always serialise the document content yourself by calling
toxml()
on the document's root element instead of thedocument
. Then you won't get an XML Declaration:...but then you also wouldn't get anything else outside the root element, such as the DOCTYPE, or any comments or processing instructions. If you need them, serialise each child of the document object one by one:
DOM Level 3 LS defines an
xml-declaration
config parameter you can use to suppress it. The only Python implementation I know of ispxdom
, which is thorough on standards support, but not at all fast.The header is print in
Document
. If you print the node directly, it won't print the header.Just replace the first line with blank: