471,091 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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
Jul 18 '05 #1
0 1373

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

303 posts views Thread by mike420 | last post: by
8 posts views Thread by Gerrit Holl | last post: by
reply views Thread by Fabian Kr?ger | last post: by
4 posts views Thread by ralphNOSPAM | last post: by
3 posts views Thread by Jon Slaughter | last post: by
2 posts views Thread by Felipe De Bene | last post: by

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.