469,332 Members | 7,055 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

xml.dom.minidom -> nextElement ?

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
Jul 18 '05 #1
5 4325
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)
***************************************
Jul 18 '05 #2
[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
<item>, it is the whitespace text node in between them. Which explains
the result printed by your code.
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,

--
alan kennedy
------------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/contact/alan
Jul 18 '05 #3
"Alan Kennedy" <al****@hotmail.com> > 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.


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
Jul 18 '05 #4
[Alexandre]
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?!)


You have to install PyXML to get Xpath support.

Try here for the software

http://pyxml.sourceforge.net/

And there is some Xpath documentation here

http://pyxml.sourceforge.net/topics/...ion-XPath.html

HTH,

--
alan kennedy
------------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/contact/alan
Jul 18 '05 #5
Thanks again for your help.
Alexandre

"Alan Kennedy" <al****@hotmail.com> a écrit dans le message de news:3F***************@hotmail.com...> You have to
install PyXML to get Xpath support.

Try here for the software

http://pyxml.sourceforge.net/

And there is some Xpath documentation here

http://pyxml.sourceforge.net/topics/...ion-XPath.html

HTH,

--
alan kennedy
------------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/contact/alan

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by xtian | last post: by
3 posts views Thread by Sunil Movva | last post: by
5 posts views Thread by Skip Montanaro | last post: by
5 posts views Thread by Mike McGavin | last post: by
6 posts views Thread by Horst Gutmann | last post: by
4 posts views Thread by webdev | last post: by
reply views Thread by Gary | last post: by
3 posts views Thread by aine_canby | last post: by
2 posts views Thread by ashmir.d | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.