473,327 Members | 1,896 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,327 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 4558
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: The output format has a header with some xml that...
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 thread. This second thread parses the message and...
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...
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 methods such as getElementsByTagNameNS(). For some...
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 to help me out.. so i started a python 2.3...
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...
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 error on XP if there is an empty namespace. The...
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 have written a script to add # -*- coding: cp1252...
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 = minidom.parse(xmlfilename) </code> The parser is...
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
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.