xml.dom.minidom - documentElement vs. childNodes

S

Skip Montanaro

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?

Thx,

Skip
 
P

Peter Otten

Skip said:
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?

Seems like comments are preserved in childNodes, too.

Peter
 
D

Diez B. Roggisch

Skip said:
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?

Maybe for reasons of orthogonality? In java, the document class implements
the node interface also. So it seems as if dom treats documents as special
cases of nodes.
 
S

Skip Montanaro

In response to:

Peter provided this interpreter session:
[<DOM Comment node "comment">, <DOM Element: root at 0x4038c1ac>]

So a document can have multiple nodes. It appears they can only have a
single Element node though:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/Users/skip/local/lib/python2.4/xml/dom/minidom.py", line 1925, in parseString
return expatbuilder.parseString(string)
File "/Users/skip/local/lib/python2.4/xml/dom/expatbuilder.py", line 940, in parseString
return builder.parseString(string)
File "/Users/skip/local/lib/python2.4/xml/dom/expatbuilder.py", line 223, in parseString
parser.Parse(string, True)
ExpatError: junk after document element: line 1, column 43
...

I guess the documentElement attribute of the dom refers to that lone Element
node.

Thanks,

Skip
 
U

Uche Ogbuji

Peter Otten said:
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"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html
xmlns:ft="http://xmlns.4suite.org/ext"
xmlns:sun="http://metadata.central/swordfish/1.0/"
xmlns="http://www.w3.org/1999/xhtml"
lang="en">
[SNIP]

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
sucks".

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 -
http://www-106.ibm.com/developerworks/edu/x-dw-xschematron-i.html
Practical (Python) SAX Notes -
http://www.xml.com/pub/a/2004/08/11/py-xml.html
XML circles the globe - http://www.javareport.com/article.asp?id=9797
Element structures for names and addresses -
http://www.ibm.com/developerworks/xml/library/x-elemdes.html
Commentary on "Objects. Encapsulation. XML?" -
http://www.adtmag.com/article.asp?id=9090
Harold's Effective XML -
http://www.ibm.com/developerworks/xml/library/x-think25.html
A survey of XML standards -
http://www-106.ibm.com/developerworks/xml/library/x-stand4/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top