xml.dom.minidom - documentElement vs. childNodes

Uche Ogbuji uche at ogbuji.net
Sat Sep 4 01:31:15 CEST 2004

Peter Otten <__peter__ at web.de> wrote in message news:<ch03lt$soa$01$1 at news.t-online.com>...
> Skip Montanaro wrote:
> > I'm getting somewhat painfully acquainted with xml.dom.minidom.  What is
> > the relationship between its documentElement attribute and its childNodes
> > list?
> > I thought XML documents consisted of a single, possibly compound, node. 
> > Why is a list of childNodes needed?
> >>> import xml.dom.minidom as md
> >>> dom = md.parseString("""<?xml version="1.0" ?><!--comment--><root/>""")
> >>> dom.childNodes
>  [<DOM Comment node "comment">, <DOM Element: root at 0x4038c1ac>]
> >>>
> Seems like comments are preserved in childNodes, too.

Comments, processing instructions and whitespace text nodes can come
before the document element, and in that way becoem the children of
the document node.  Minidom doesn't keep document-level whitespace,
but som DOM impls do.

Furthermore the doctype of the document is a child of the document
node.  It can be an actual doc type declaration:

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

Or an implicit one, as in your example (where the only doctype
information is the document type element).

All that having been said, I must add that in the words of Guido "DOM

We designed Domlette to ignore some of the madness of the full DOM
spec (though it does support multiple children for the document node,
which is pwerfectly fine and useful).

Uche Ogbuji                                    Fourthought, Inc.
http://uche.ogbuji.net    http://4Suite.org    http://fourthought.com
Meet me at XMLOpen Sept 21-23 2004, Cambridge, UK.  http://xmlopen.org
A hands-on introduction to ISO Schematron -
Practical (Python) SAX Notes -
XML circles the globe - http://www.javareport.com/article.asp?id=9797
Element structures for names and addresses -
Commentary on "Objects. Encapsulation. XML?" -
Harold's Effective XML -
A survey of XML standards -

More information about the Python-list mailing list