xml.dom.minidom childnodes

V

Vincent De Baere

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
 
M

Mark McEahern

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
 
V

Vincent De Baere

Mark said:
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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top