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
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