472,353 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

xml.dom.minidom - documentElement vs. childNodes

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
Jul 18 '05 #1
4 5211
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.

Peter

Jul 18 '05 #2
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?


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.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #3
In response to:
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?
Peter provided this interpreter session:
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>]

So a document can have multiple nodes. It appears they can only have a
single Element node though:
dom = md.parseString("""<?xml version="1.0" ?><!--comment--><root/><root2/>""")

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
Jul 18 '05 #4
Peter Otten <__*******@web.de> wrote in message news:<ch*************@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"
"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/developerwork...ematron-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/xm...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/xm...x-think25.html
A survey of XML standards -
http://www-106.ibm.com/developerwork...rary/x-stand4/
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vincent De Baere | last post by:
Hi I am playing around a little with python (I've started using python at 1 PM today, so I guess I could be considered a newbie :-)), and have...
5
by: Skip Montanaro | last post by:
I'd like to compare two xml.dom.minidom objects, but the naive attempt fails: >>> import xml.dom.minidom >>> d1 = xml.dom.minidom.parse("ES.xml")...
1
by: Greg Wogan-Browne | last post by:
Hi all, I am having some trouble figuring out what is going on here - is this a bug, or correct behaviour? Basically, when I create an XML...
4
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again...
1
by: Dean Card | last post by:
I am using minidom to parse a 20,000 line XML file. I have a few instances where the number of child nodes of a particular node can be variable in...
4
by: bkamrani | last post by:
Great guys: As a newbie, I'm trying to simply parse a xml file using minidom, but I don't know why I get some extra children(?). I don't know...
1
by: Maksim Kasimov | last post by:
Hi, i'm faced with such a problem when i use xml.dom.minidom: to append all child nodes from "doc" in "_requ" to "doc" in "_resp", i do the...
18
by: sim.sim | last post by:
Hi all. i'm faced to trouble using minidom: #i have a string (xml) within CDATA section, and the section includes "\r\n": iInStr = '<?xml...
13
by: ArseAssassin | last post by:
I'm using minidom to parse XML and to simplify accessing child nodes, I'm trying to implement __getattr__ for the Node class. Yes, I know what most...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.