help needed creating xml document with xml.dom

Martin v. Loewis martin at v.loewis.de
Fri Sep 6 18:11:10 CEST 2002


Brian Lalor <blalor at insight.com> writes:

> Good morning, all.  Several times, now, I've used xml.dom.minidom to create
> raw XML documents, but I don't think I've ever really done it the "right"
> way.  

I think that trying to create valid XML documents with DOM is
inherently wrong. Use print statements instead.

> I blame that on my seeming inability to find documentation on the
> "right" way to do it in Python, or in any language, for that matter.
> Pointers to said documentation are welcomed. :-)

Unfortunately, the DOM does not provide a way to create a Document
with an internal subset.

> Using minidom, I cannot seem to get either the namespace or doctype
> to print out.

Adding namespaces should be possible; adding a DOCTYPE declaration is
not.

>     # is this the right way, instead of directly importing xml.dom.minidom?
>     DOM = xml.dom.getDOMImplementation()

Yes, that's correct.

>     index_page.documentElement.setAttribute('xml:lang', 'en')
>     index_page.documentElement.setAttribute('lang', 'en')

If you want namespaces, you should use the namespace API:
setAttributeNS, createElementNS.

> Is this a bug in minidom, or (more likely) am I doing something wrong?  

Call it a bug. It's really a feature missing in DOM - while it *may*
work for an external DTD, there is no way to get it work with an
internal subset.

Again, using plain print statements will be easier.

Regards,
Martin



More information about the Python-list mailing list