How to add xml header to dom object

I am using Python xml.dom.minidom, but I think the question is valid for any DOM parser.

In my source file there is a line at the beginning:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

This is not like the dom part, so when I do something like dom.toxml (), the resulting line does not have a line at the beginning.

How to add it?

outpupt example:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root xmlns:aid="http://xxxxxxxxxxxxxxxxxx">
<Section>BANDSAW BLADES</Section>
</Root>

hope to be clear.

+3
source share
1 answer

This does not seem to be part of dom

The XML declaration does not get its own node, but the properties declared in it are visible in the object Document:

>>> doc= minidom.parseString('<?xml version="1.0" encoding="utf-8" standalone="yes"?><a/>')
>>> doc.encoding
'utf-8'
>>> doc.standalone
True

standalone="yes", toxml() . , , , toxml() - promises XML . (, encoding, .)

:

xml= []
xml.append('<?xml version="1.0" encoding="utf-8" standalone="yes"?>')
for child in doc.childNodes:
    xml.append(child.toxml())

XML ? , DOCTYPE, , . XML: " , ". , .

+2

Source: https://habr.com/ru/post/1770629/


All Articles