Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an XML file without header in Python?

Tags:

python

xml

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

like image 338
Nico Schlömer Avatar asked May 29 '10 00:05

Nico Schlömer


3 Answers

The header is print in Document. If you print the node directly, it won't print the header.

root = doc.childNodes[0]
root.toprettyxml(encoding="utf-8")
like image 113
Joshua Avatar answered Oct 13 '22 00:10

Joshua


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 the document. Then you won't get an XML Declaration:

xml= document.documentElement.toxml('utf-8')

...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:

xml= '\n'.join(node.toxml('utf-8') for node in document.childNodes)

I wondered if there are other packages which do allow to neglect the header.

DOM Level 3 LS defines an xml-declaration config parameter you can use to suppress it. The only Python implementation I know of is pxdom, which is thorough on standards support, but not at all fast.

like image 37
bobince Avatar answered Oct 12 '22 22:10

bobince


Just replace the first line with blank:

import xml.dom.minidom as MD     
<XML String>.replace(MD.Document().toxml()+'\n', '') 
like image 31
Ravish Bharti Avatar answered Oct 12 '22 23:10

Ravish Bharti