Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 29th, 2004, 08:59 PM
rockets12345 rockets12345 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 101 rockets12345 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 50 m 20 sec
Reputation Power: 5
XML Parsing problem using DOM Parser: HELP NEEDED

Hi,
I have a problem and can't figure it out and need your help, please look at the following code and the output also a xml file snippet is down there too.
Looking at the output I don't know why it's not according to the code.

Following code is pretty much clear: When method starts execution <Pat> Node is passed it has childNodes
Inside for-loop
if x.localName == 'id': check for validTine and calls the same method again,
now executes elif x.localName == 'validTime': check for the appropriate childNode and calls the method again,
now inside if temp_node.localName == 'low': calls the method but instead of just executing elif x.localName == 'low' and x.parentNode.localName == 'validTime':
and printing only
print " In low"
it prints all the rest too i.e.
print "In high"
print "In high1"
print "In high2"
print "In high3"
print "In high4"
print "In high5"

then again in the for-loop of validTime this time executes elif temp_node.localName == 'high': but instead of
just executing elif x.localName == 'high' and x.parentNode.localName == 'validTime':
and printing only
print "In high"
it prints all the rest too i.e.
print "In low"
print "In high1"
print "In high2"
print "In high3"
print "In high4"
print "In high5"

and it continues for the rest.

Why it when executing
if temp_node.localName == 'low':
print " **** low"
self.iterate(x)
only executes
elif x.localName == 'low' and x.parentNode.localName == 'validTime':
print " In low"

and so on. Why it iterates through all the rest elements too and when the condition for one is being checked.
Is it the normal way that xml parsing is handled in python. If so what's the best way
to handle this situation using DOM(Parser). As I need to parse the xml-file it's pretty big and each Node
can have nested childNodes and so forth as the example shows. But anytime parsing the Node doesn't stops and jumps to the right condition
as shown in the following sample: Any help is appreciated


def iterate(self,node):
print '0 === %s' % node.localName

for x in node.childNodes:
if x.nodeType == Node.TEXT_NODE:
pass
else:
print "1 **** %s" % x.localName

if x.localName == 'id':
print " **** id"
for temp_node in x.childNodes:
if temp_node.localName == 'validTime':
self.iterate(x)

elif x.localName == 'validTime':
for temp_node in x.childNodes:
if temp_node.localName == 'low':
print " **** low"
self.iterate(x)
elif temp_node.localName == 'high':
print " **** low"
self.iterate(x)
elif temp_node.localName == 'high1':
print " **** high1"
self.iterate(x)
elif temp_node.localName == 'high2':
print " **** high2"
self.iterate(x)
elif temp_node.localName == 'high3':
print " **** high3"
self.iterate(x)
elif temp_node.localName == 'high4':
print " **** high4"
self.iterate(x)
elif temp_node.localName == 'high5':
print " **** high5"
self.iterate(x)
else:
pass

elif x.localName == 'low' and x.parentNode.localName == 'validTime':
print " In low"
elif x.localName == 'high' and x.parentNode.localName == 'validTime':
print "In high"
elif x.localName == 'high1' and x.parentNode.localName == 'validTime':
print "In high1"
elif x.localName == 'high2' and x.parentNode.localName == 'validTime':
print "In high2"
elif x.localName == 'high3' and x.parentNode.localName == 'validTime':
print "In high3"
elif x.localName == 'high4' and x.parentNode.localName == 'validTime':
print "In high4"
elif x.localName == 'high5' and x.parentNode.localName == 'validTime':
print "In high5"


*********** Following is the snippet of the .xml file ***************
<Pat>
<id>
<validTime>
<low val="l"/>
<high val="12"/>
<high1 val="123"/>
<high2 val="1234"/>
<high3 val="12345"/>
<high4 val="123456"/>
<high5 val="1234567"/>
</validTime>
</id>
<Person/>
<Organ>
<id/>
</Organ>
</Pat>

************* Following is the output from execution of the method ************
0 === Pat
1 **** id
0 === id
1 **** validTime
**** low
0 === validTime
1 **** low
In low
1 **** high
In high
1 **** high1
In high1
1 **** high2
In high2
1 **** high3
In high3
1 **** high4
In high4
1 **** high5
In high5
**** high
0 === validTime
1 **** low
In low
1 **** high
In high
1 **** high1
In high1
1 **** high2
In high2
1 **** high3
In high3
1 **** high4
In high4
1 **** high5
In high5
**** high1
0 === validTime
1 **** low
In low
1 **** high
In high
1 **** high1
In high1
1 **** high2
In high2
1 **** high3
In high3
1 **** high4
In high4
1 **** high5
In high5
**** high2
0 === validTime
1 **** low
In low
1 **** high
In high
1 **** high1
In high1
1 **** high2
In high2
1 **** high3
In high3
1 **** high4
In high4
1 **** high5
In high5
**** high3
0 === validTime
1 **** low
In low
1 **** high
In high
1 **** high1
In high1
1 **** high2
In high2
1 **** high3
In high3
1 **** high4
In high4
1 **** high5
In high5
**** high4
0 === validTime
1 **** low
In low
1 **** high
In high
1 **** high1
In high1
1 **** high2
In high2
1 **** high3
In high3
1 **** high4
In high4
1 **** high5
In high5
**** high5
0 === validTime
1 **** low
In low
1 **** high
In high
1 **** high1
In high1
1 **** high2
In high2
1 **** high3
In high3
1 **** high4
In high4
1 **** high5
In high5
1 **** Person
1 **** Organ

Reply With Quote
  #2  
Old May 31st, 2004, 02:38 PM
rockets12345 rockets12345 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 101 rockets12345 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 50 m 20 sec
Reputation Power: 5
Quote:
Originally Posted by rockets12345
Hi,
I have a problem and can't figure it out and need your help, please look at the following code and the output also a xml file snippet is down there too.
Looking at the output I don't know why it's not according to the code.

Following code is pretty much clear: When method starts execution <Pat> Node is passed it has childNodes
Inside for-loop
if x.localName == 'id': check for validTine and calls the same method again,
now executes elif x.localName == 'validTime': check for the appropriate childNode and calls the method again,
now inside if temp_node.localName == 'low': calls the method but instead of just executing elif x.localName == 'low' and x.parentNode.localName == 'validTime':
and printing only
print " In low"
it prints all the rest too i.e.
print "In high"
print "In high1"
print "In high2"
print "In high3"
print "In high4"
print "In high5"

then again in the for-loop of validTime this time executes elif temp_node.localName == 'high': but instead of
just executing elif x.localName == 'high' and x.parentNode.localName == 'validTime':
and printing only
print "In high"
it prints all the rest too i.e.
print "In low"
print "In high1"
print "In high2"
print "In high3"
print "In high4"
print "In high5"

and it continues for the rest.

Why it when executing
if temp_node.localName == 'low':
print " **** low"
self.iterate(x)
only executes
elif x.localName == 'low' and x.parentNode.localName == 'validTime':
print " In low"

and so on. Why it iterates through all the rest elements too and when the condition for one is being checked.
Is it the normal way that xml parsing is handled in python. If so what's the best way
to handle this situation using DOM(Parser). As I need to parse the xml-file it's pretty big and each Node
can have nested childNodes and so forth as the example shows. But anytime parsing the Node doesn't stops and jumps to the right condition
as shown in the following sample: Any help is appreciated


def iterate(self,node):
print '0 === %s' % node.localName

for x in node.childNodes:
if x.nodeType == Node.TEXT_NODE:
pass
else:
print "1 **** %s" % x.localName

if x.localName == 'id':
print " **** id"
for temp_node in x.childNodes:
if temp_node.localName == 'validTime':
self.iterate(x)

elif x.localName == 'validTime':
for temp_node in x.childNodes:
if temp_node.localName == 'low':
print " **** low"
self.iterate(x)
elif temp_node.localName == 'high':
print " **** low"
self.iterate(x)
elif temp_node.localName == 'high1':
print " **** high1"
self.iterate(x)
elif temp_node.localName == 'high2':
print " **** high2"
self.iterate(x)
elif temp_node.localName == 'high3':
print " **** high3"
self.iterate(x)
elif temp_node.localName == 'high4':
print " **** high4"
self.iterate(x)
elif temp_node.localName == 'high5':
print " **** high5"
self.iterate(x)
else:
pass

elif x.localName == 'low' and x.parentNode.localName == 'validTime':
print " In low"
elif x.localName == 'high' and x.parentNode.localName == 'validTime':
print "In high"
elif x.localName == 'high1' and x.parentNode.localName == 'validTime':
print "In high1"
elif x.localName == 'high2' and x.parentNode.localName == 'validTime':
print "In high2"
elif x.localName == 'high3' and x.parentNode.localName == 'validTime':
print "In high3"
elif x.localName == 'high4' and x.parentNode.localName == 'validTime':
print "In high4"
elif x.localName == 'high5' and x.parentNode.localName == 'validTime':
print "In high5"


*********** Following is the snippet of the .xml file ***************
<Pat>
<id>
<validTime>
<low val="l"/>
<high val="12"/>
<high1 val="123"/>
<high2 val="1234"/>
<high3 val="12345"/>
<high4 val="123456"/>
<high5 val="1234567"/>
</validTime>
</id>
<Person/>
<Organ>
<id/>
</Organ>
</Pat>

************* Following is the output from execution of the method ************
0 === Pat
1 **** id
0 === id
1 **** validTime
**** low
0 === validTime
1 **** low
In low
1 **** high
In high
1 **** high1
In high1
1 **** high2
In high2
1 **** high3
In high3
1 **** high4
In high4
1 **** high5
In high5
**** high
0 === validTime
1 **** low
In low
1 **** high
In high
1 **** high1
In high1
1 **** high2
In high2
1 **** high3
In high3
1 **** high4
In high4
1 **** high5
In high5
**** high1
0 === validTime
1 **** low
In low
1 **** high
In high
1 **** high1
In high1
1 **** high2
In high2
1 **** high3
In high3
1 **** high4
In high4
1 **** high5
In high5
**** high2
0 === validTime
1 **** low
In low
1 **** high
In high
1 **** high1
In high1
1 **** high2
In high2
1 **** high3
In high3
1 **** high4
In high4
1 **** high5
In high5
**** high3
0 === validTime
1 **** low
In low
1 **** high
In high
1 **** high1
In high1
1 **** high2
In high2
1 **** high3
In high3
1 **** high4
In high4
1 **** high5
In high5
**** high4
0 === validTime
1 **** low
In low
1 **** high
In high
1 **** high1
In high1
1 **** high2
In high2
1 **** high3
In high3
1 **** high4
In high4
1 **** high5
In high5
**** high5
0 === validTime
1 **** low
In low
1 **** high
In high
1 **** high1
In high1
1 **** high2
In high2
1 **** high3
In high3
1 **** high4
In high4
1 **** high5
In high5
1 **** Person
1 **** Organ

Any ideas

Reply With Quote
  #3  
Old June 3rd, 2004, 10:22 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
Please use a code block then someone may be able to help.
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > XML Parsing problem using DOM Parser: HELP NEEDED


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT