PyXML DOM question: adding the DOCTYPE

Preston Landers pibble at
Mon Aug 13 21:01:38 CEST 2001

Hi all.

I'm building XML documents in Python 2.1 using xml.dom.minidom.

Everything is peachy keen with the output except one thing: I need a
<!DOCTYPE> meta-element at the top of the document, and I can't seem
to get minidom to include this.  The scanty documentation provides few
clues for this puzzle.

I'm using a DocumentType object received from createDocumentType, and
I specify the DTD in that, but it doesn't want to create a DOCTYPE
element in the final output.

Am I going to have to do something ugly and hackish like manually
inserting my <!DOCTYPE> in my final XML string with a regular
expression? Bah... there has to be a better way!!


from xml.dom import minidom

# DO == DocumentObjectModel Implementation object
DO = minidom.getDOMImplementation()

# DT == Document Type object
DT = DO.createDocumentType('JxRule', '', 'jxrule.dtd')
doc = DO.createDocument("", "JxRule", DT)


print doc.toxml()

More information about the Python-list mailing list