开发者

Set a DTD using minidom in python

I am trying to include a reference to a DTD in my XML doc using minidom.

I am creating the document like:

doc = Document()
foo = doc.createElement('foo')
doc.appendChild(foo)
doc.toxml()

This gives me:

<?xml version="1.0" ?>
<foo/>

I need to get something like:

<?xml version="1.0" ?>
<!DOCTYPE something开发者_运维问答 SYSTEM "http://www.path.to.my.dtd.com/my.dtd">
<foo/>


The documentation is out of date. Use the source, Luke. I do it something like this.

from xml.dom.minidom import DOMImplementation

imp = DOMImplementation()
doctype = imp.createDocumentType(
    qualifiedName='foo',
    publicId='', 
    systemId='http://www.path.to.my.dtd.com/my.dtd',
)
doc = imp.createDocument(None, 'foo', doctype)
doc.toxml()

This prints the following.

<?xml version="1.0" ?><!DOCTYPE foo  SYSTEM \'http://www.path.to.my.dtd.com/my.dtd\'><foo/>

Note how the root element is created automatically by createDocument(). Also, your 'something' has been changed to 'foo': the DTD needs to contain the root element name itself.


According to the Python docs, there is no implementation of the DocumentType interface in the minidom.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜