Hi all,

I'm trying to read data from this xml file that looks like this:

<VEGETABLE >
< FRUIT >
<FRUIT name="apple">1</FRUIT>
<FRUIT name="Orange">2</FRUIT>
<FRUIT name="Benana">6</FRUIT>


I wrote the following script:

import sys
from xml.dom.minidom import parse
from cmath import sqrt

# Load XML into tree structure
tree = parse(sys.stdin)

#Find all  <FRUITS>
pa_list=tree.getElementsByTagName('VEGETABLE')[0].getElementsByTagName('FRUITS')[0].getElementsByTagName('FRUITS')

#Find the 'T' node in the list of FRUITS
for pa in pa_list:
    try:
        if pa.attributes.getNamedItem('name').nodeValue == 'apple':
           fru = pa
           break
    except:
        pass

fruitvalue=0
fruitvalue=(float(fru.getElementsByTagName('apple').childNodes.data))

print "\t%g"% (fruitvalue)

I'm getting this error:

fruitvalue=(float(fru.getElementsByTagName('apple').childNodes.data))
NameError: name 'fru' is not defined

but I believable I'm defining it by: fru = pa.
Any help is extremely appreciated.

Edited 5 Years Ago by SakuraPink: n/a

You defined fru only if a test suceeded. What you should do is replace lines 12-18 with

fru = None # give an initial dummy value to fru
for pa in pa_list:
    try:
        nodevalue = pa.attributes.getNamedItem('name').nodeValue
        print "nodevalue: %s" % str(nodevalue) # for debugging purposes
        if nodevalue == 'apple':
           fru = pa
           break
    except Exception: # <-- replace Exception by a more specific exception later
        raise # don't catch the exception first to see what happens
if fru is None:
    # handle the case where apple was not found
    raise RuntimeError("'apple' was not found")

Edited 5 Years Ago by Gribouillis: n/a

Gribouillis Thanks for reply.I'm sure there is a <FRUIT name="apple">1</FRUIT> in my file. However I made the change and now I'm getting this error:


raise RuntimeError("'apple' was not found")
RuntimeError: 'apple' was not found

Gribouillis Thanks for reply.I'm sure there is a <FRUIT name="apple">1</FRUIT> in my file. However I made the change and now I'm getting this error:


raise RuntimeError("'apple' was not found")
RuntimeError: 'apple' was not found

Beware that some tags are FRUITS and other are FRUIT. Here is a modified code that works for me

# python 2 - fruits.py -
import sys
from xml.dom.minidom import parse
from cmath import sqrt
from cStringIO import StringIO

xml_data = """
<VEGETABLE >
<FRUITS>
<FRUIT name="apple">1</FRUIT>
<FRUIT name="Orange">2</FRUIT>
<FRUIT name="Benana">6</FRUIT>
</FRUITS>
</VEGETABLE>
"""
input_file = StringIO(xml_data)

# Load XML into tree structure
tree = parse(input_file)

#Find all  <FRUITS>
pa_list=tree.getElementsByTagName('VEGETABLE')[0].getElementsByTagName('FRUITS')[0].getElementsByTagName('FRUIT')

#Find the 'T' node in the list of FRUITS
fru = None # give an initial dummy value to fru
for pa in pa_list:
    try:
        nodevalue = pa.attributes.getNamedItem('name').nodeValue
        print "nodevalue: %s" % str(nodevalue) # for debugging purposes
        if nodevalue == 'apple':
           fru = pa
           break
    except Exception: # <-- replace Exception by a more specific exception later
        raise # don't catch the exception first to see what happens
if fru is None:
    # handle the case where apple was not found
    raise RuntimeError("'apple' was not found")

fruitvalue=0
fruitvalue=float(fru.childNodes[0].data)

print "\t%g"% (fruitvalue)

"""my output -->
nodevalue: apple
	1
"""

Also, usually, I parse xml using the lxml library (http://lxml.de/). Did you consider this option ?

Edited 5 Years Ago by Gribouillis: n/a

That Fruit vs Fruits thing was the problem! Thanks for your help. :)
Thanks for the advice I will look at lxml lib. But can you tell me about the difference?

This question has already been answered. Start a new discussion instead.