XML with Python - supposed to be simple? ;)

Martin v. Loewis martin at v.loewis.de
Mon Jul 22 01:25:49 EDT 2002


"Vladimir Cherepanov" <vovka_foreverREMOVE_THIS at hotmail.com> writes:

> I skimmed through some w3c documentation on DOM, but didn't find anything
> about creating documents, apart from the statement that "it's
> implementation-specific" (why?!). Most w3c documentation concerns more
> advanced topics like XML parsing, etc.

Where did you read that it is implementation-specific?

In the DOM, you have the general problem that it only specifies
interfaces, not implementations. Implementation classes are, by
nature, implementation-specific, and so are their class names. To
create an instance of a class, you need the class name in most
languages - that makes it implementation-specific.

In most cases, the DOM solves this problem by providing factory
functions. In DOM Level 1, there is no factory for creating Documents,
hence creating them is implementation-specific.

In DOM Level 2, there is the DOMImplementation interface, which makes
creation of Document nodes portable. There is still no portable way to
parse a document, or to serialize it.

Those capabilities become available in DOM Level 3, which is not yet
completely specified, and unimplemented in PyXML.

> > You'll then learn that you can create
> > Document instances by invoking createDocument on the
> > DOMImplementation. For minidom, you get the DOM implementation by
> > calling xml.dom.minidom.getDOMImplementation().
> 
> Thanks, I'll try this. Btw, I didn't find this method in DOMImplementation
> Interface - the only method I found was "hasFeature()".

It is a method to obtain a DOMImplementation object, so it cannot be
an operation of the DOMImplementation - to call it, you would need a
DOMImplementation first, and then this operation would be pointless.

Finding DOM implementations is still implementation-specific, even in
DOM level 3. PyXML adds a Python-specific registry for DOM
implementations, via xml.dom.getDOMImplementation. This operates on a
number of registered DOM implementations, invoking some module-level
getDOMImplementation functions.

> And what about serializing?

Again, implementation-specific (in DOM Level 3, part of the
load-store-specification). In PyXML, toxml() and writexml() can be
useful.

Regards,
Martin



More information about the Python-list mailing list