xml.dom.minidom -> nextElement ?

A

Alexandre

Hello all,

Could someone explain to me why there is no nextElement in minidom ?

if i execute this :
***************************************
import xml.dom.minidom
doc = """\
<root>
<item>content1</item>
<item>content2</item>
</root>"""
dom = xml.dom.minidom.parseString(doc)
firstItem = dom.getElementsByTagName("item")[0]
nextItem = firstItem.nextSibling
print nextItem
***************************************
the result is :
<DOM Text node "
">

so if i want the next element i could use "firstItem.nextSibling.nextSibling"

or write my own nextElement :
***************************************
def nextElement(current):
pointer = current.nextSibling
if pointer.nodeType == pointer.ELEMENT_NODE:
return pointer
elif pointer == None:
return None
else: return getNextElement(pointer)
***************************************
But i'm wondering if i am not missing something obvious ?
Thx in advance for your help.
Alexandre
 
A

Alexandre

little correction, next time ill test before sending :)
***************************************
def nextElement(current):
next = current.nextSibling
if next == None: return next
elif next.nodeType == next.ELEMENT_NODE:
return next
else: return nextElement(next)
***************************************
 
A

Alan Kennedy

[Alexandre]
Could someone explain to me why there is no nextElement in minidom ?

if i execute this :
***************************************
import xml.dom.minidom
doc = """\
<root>
<item>content1</item>
<item>content2</item>
</root>"""
dom = xml.dom.minidom.parseString(doc)
firstItem = dom.getElementsByTagName("item")[0]
nextItem = firstItem.nextSibling
print nextItem
***************************************
the result is :
<DOM Text node "
">

You're possibly being confused by the fact that the <root> element has
*5* children, not 2. There are the 2 obvious children, <item>s 1 and
2. However, the whitespace

o between <root> and <item>
o between </item> and <item>
o between </item> and </root>

also create text nodes, which only contain whitespace.

Note also that the nextSibling of the first <item> is *not* the second
it is the whitespace text node in between them. Which explains said:
so if i want the next element i could use "firstItem.nextSibling.nextSibling"

or write my own nextElement :
***************************************
def nextElement(current):
pointer = current.nextSibling
if pointer.nodeType == pointer.ELEMENT_NODE:
return pointer
elif pointer == None:
return None
else: return getNextElement(pointer)
***************************************

Well, you could

1. Navigate over your tree deleting whitespace nodes, before doing
your processing.
2. Write your own nav function which skips whitespace text nodes (as
you have done above).
3. Use Xpath to find nodes in trees.
But i'm wondering if i am not missing something obvious ?
Thx in advance for your help.

HTH,
 
A

Alexandre

Alan Kennedy said:
1. Navigate over your tree deleting whitespace nodes, before doing
your processing.
2. Write your own nav function which skips whitespace text nodes (as
you have done above).
3. Use Xpath to find nodes in trees.

Alan, thanks a lot for your answer.
It just raises another question :)
Using Xpath sounds realy cool but... can i do that with a DOM object ?
(haven't found any reference to Xpath in DOM or minidom Python's doc?!)

Alexandre
 

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
474,172
Messages
2,570,933
Members
47,473
Latest member
ChristelPe

Latest Threads

Top