473,890 Members | 1,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5323
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.pa rseString(strin g)
File "/Users/skip/local/lib/python2.4/xml/dom/expatbuilder.py ", line 940, in parseString
return builder.parseSt ring(string)
File "/Users/skip/local/lib/python2.4/xml/dom/expatbuilder.py ", line 223, in parseString
parser.Parse(st ring, 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.or g/ext"
xmlns:sun="http ://metadata.centra l/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
3279
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 come across a little problem regarding xml.dom.minidom. this code snippet: menudoc = xml.dom.minidom.parse("menu.xml") menurootchildren = menudoc.documentElement.childNodes
5
4833
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") >>> d2 = xml.dom.minidom.parse("ES.xml") >>> d1 == d2 False My goal is to decide whether or not I need to prompt the user to save config information at the end of a program run by generating a minidom object then
1
2779
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 document with a namespace using xml.dom.minidom.parse() or parseString(), the namespace exists as an xmlns attribute in the DOM (fair enough, as it's in the original source document). However, if I use the DOM implementation to create an identical document with a namespace, the xmlns attribute is not...
4
6078
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 to help me out.. so i started a python 2.3 script that grabs some web pages from the web, regex parse the data and stores it localy to xml file for further use.. at first i had no problem using python minidom and everything concerning
1
1641
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 number. To access them I am doing something like the following... xmldoc = minidom.parseString(r) results = xmldoc.childNodes for myNode in results.childNodes.childNodes: do Stuff with myNode...
4
2028
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 what is wrong in xml file, but I've tried different xml files, still same problem. ****************************************************************************** xml file (fileTest) looks like: <?xml version="1.0" encoding="ISO-8859-1" ?>
1
1981
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 following: _requ = minidom.parseString("<resp><doc><one>One</one><two>Two</two></doc></resp>") _resp = minidom.parseString("<resp><doc/></resp>") iSourseTag = _requ.getElementsByTagName('doc') iTargetTag = _resp.getElementsByTagName('doc')
18
758
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 version="1.0"?>\n<Data><!]></Data>\n' #After i create DOM-object, i get the value of "Data" without "\r\n"
13
2152
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 of you will think of that; I'll just have to be careful, won't I. ;) import xml from xml.dom.minidom import parseString def getChild(self, name): print "Getting child" return self.getElementsByTagName(name) xml.dom.minidom.Node.__getattr__ = getChild
0
10814
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9630
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8016
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7169
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5849
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4676
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4270
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.