[XML-SIG] great python xml frustrations

Andrew Clover and-xml at doxdesk.com
Fri Jan 23 18:33:30 EST 2004

bauer at mit.edu wrote:

> I want to add elements from different documents to create
> a new dom.

> from xml.dom import Document
> import xml.dom.minidom

Look out! You aren't just using different documents there - you're
using different DOM implementations. xml.dom.Document comes from 4DOM,
an implementation unconnected to minidom (other than them both being
distributed in the _xmlplus package).

The DOM standard does not guarantee that it should be possible to
import a node from one implementation to another. It's technically
possible to implement, but neither minidom nor 4DOM do so for now.

> newdoc = Document.Document(None)

Instantiating a class directly like this isn't generally a good idea:
constructors are not a part of the DOM standard, so what they do is
implementation-specific and can cause unforeseen problems in Python
DOMs. Instead, use the createDocument method on a DOMImplementation.

For example, to create a new minidom Document:

  imp= xml.dom.minidom.getDOMImplementation()
  doc= imp.createDocument(None, 'rootelname', None)

See also xml.dom.domreg.

Andrew Clover
mailto:and at doxdesk.com

More information about the XML-SIG mailing list