473,320 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

xml.dom.minidom childnodes

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
menunode = menurootchildren[1].cloneNode(1)
sys.stdout.write(menunode.toprettyxml(" "))

acts on this file:

<?xml version="1.0" encoding="UTF-8"?>
<menu>
<ol>
<li>test</li>
<li>test2</li>
<li><ol><li>test3</li></ol></li>
</ol>
<ol>
<li>please don't display this</li>
</ol>
</menu>

The thing I got from the manual is that lists (such as menurootchildren)
are zerobased.

As you can see in the xml file, there is a newline after the (root)
documentelement. I guess that's why I need to use menurootchildren[1]
instead of menurootchildren[0] to access the first child.

I was wondering how I could get the child elements (without the text
nodes) instead of the childnodes, thus simply accessing the first child
(first occurance of ol) element by using menurootchildren[0]. That's
because I would very much like the script to come up with the same
result regardless of if there is a newline after the root element or not...

TIA

Vincent
Jul 18 '05 #1
2 3199
On Sun, 2004-01-18 at 11:04, Vincent De Baere wrote:
As you can see in the xml file, there is a newline after the (root)
documentelement. I guess that's why I need to use menurootchildren[1]
instead of menurootchildren[0] to access the first child.


Why not use getElementsByTagName and punt on the whole variable
whitespace as childNodes business?

Cheers,

// m
Jul 18 '05 #2
Mark McEahern wrote:
On Sun, 2004-01-18 at 11:04, Vincent De Baere wrote:

As you can see in the xml file, there is a newline after the (root)
documentelement. I guess that's why I need to use menurootchildren[1]
instead of menurootchildren[0] to access the first child.


Why not use getElementsByTagName and punt on the whole variable
whitespace as childNodes business?

That seems to be the solution indeed! Thank you!

Vincent

Jul 18 '05 #3

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

Similar topics

4
by: Skip Montanaro | last post by:
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...
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") >>> d2 = xml.dom.minidom.parse("ES.xml") >>> d1...
4
by: Derek Basch | last post by:
Hello All, I ran into a problem while dynamically constructing XHTML documents using minidom. If you create a script tag such as: script_node_0 = self.doc.createElement("script")...
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 to help me out.. so i started a python 2.3...
2
by: Marco | last post by:
Hello! Can anyone explain why the following code does not work? (I'm using python2.4.) Cheers, Marco --
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 number. To access them I am doing something like...
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 following: _requ =...
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 version="1.0"?>\n<Data><!]></Data>\n' #After i...
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 of you will think of that; I'll just have to be...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.