472,353 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 4478
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: xtian | last post by:
Hi - I'm doing some data conversion with minidom (turning a csv file into a specific xml format), and I've hit a couple of small problems. 1:...
3
by: Sunil Movva | last post by:
I have an application that uses xml to communicate between threads. One of the threads in my app creates an xml message and sends it to a second...
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")...
5
by: Mike McGavin | last post by:
Hi everyone. I've been trying for several hours now to get minidom to parse namespaces properly from my stream of XML, so that I can use DOM...
6
by: Horst Gutmann | last post by:
Hi :-) I currently have quite a big problem with minidom and special chars (for example &uuml;) in HTML. Let's say I have following input file:...
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...
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...
0
by: Gary | last post by:
Howdy I ran into a difference between Python on Windows XP and Linux Fedora 6. Writing a dom to xml with minidom works on Linux. It gives an...
3
by: aine_canby | last post by:
Hi, I'm working with a number of scripts which were written years ago for my company for Python 2.2, and I'd like to update for Python 2.5. I...
2
by: ashmir.d | last post by:
Hi, I am trying to parse an xml file using the minidom parser. <code> from xml.dom import minidom xmlfilename = "sample.xml" xmldoc =...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.