How to write an XML file without a header in Python?

when using XML tools in a Python warehouse, such as xml.dom.minidom to write XML, the file always runs like

<?xml version="1.0"?>

[...]

While this is completely legal XML code, and it is even recommended that you use the header, I would like to get rid of it, because one of the programs I'm working with has problems.

I can not find a suitable option in xml.dom.minidom , so I wondered if there are other packages that let me ignore the header.

Greetings

Niko

+7
python xml
source share
7 answers

Unfortunately, minidom you from omitting the XML Declaration.

But you can always serialize the contents of the document yourself by calling toxml() on the root element of the document instead of document . Then you will not get the XML declaration:

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

... but then you also won’t get anything else outside the root element, such as DOCTYPE, or any comments or processing instructions. If you need it, serialize each child of the document one by one:

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

I wondered if there are any other packages that let you neglect the header.

DOM Level 3 LS defines an xml-declaration config parameter that you can use to suppress it. The only Python implementation I know of is pxdom , which is fully standards compliant, but not very fast.

+15
source share

If you want to use the mini-mini and maintain the “prettiness”, how about this as a quick / hacker 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 
+5
source share

Purists may not like to hear this, but I found using an XML parser to generate XML, which would be redundant. Just generate it directly as strings. It also allows you to create files that are larger than you can store in memory, which you cannot do with the DOM. Reading XML is another story.

0
source share

If you want to use the mini-disk, just scan it back to the file and delete the first line after writing all the XML you need.

0
source share

You might be able to use a custom file-like object that removes the first tag, for example:

 class RemoveFirstLine: def __init__(self, f): self.f = f self.xmlTagFound = False def __getattr__(self, attr): return getattr(self, self.f) def write(self, s): if not self.xmlTagFound: x = 0 # just to be safe for x, c in enumerate(s): if c == '>': self.xmlTagFound = True break self.f.write(s[x+1:]) else: self.f.write(s) ... f = RemoveFirstLine(open('path', 'wb')) Node.writexml(f, encoding='UTF-8') 

or something similar. This has the advantage that the file does not have to be completely rewritten if the XML files are quite large.

0
source share

The title is printed in Document . If you print the node directly, it will not print the header.

 root = doc.childNodes[0] root.toprettyxml(encoding="utf-8") 
0
source share

Just replace the first line with an empty one:

 import xml.dom.minidom as MD <XML String>.replace(MD.Document().toxml()+'\n', '') 
0
source share

All Articles