Compare node value from xml with String in Python
Question: After searching a lot and trying a lot I am not able to compare the node from
xml with string entered from User in python script. I hope it is due to a type
mismatch because I am getting value from XML in unicode format, please suggest
the way ASAP to compare two strings successfully. Thanks in Advance.

My Python script:

    
    
    from  xml.dom.minidom import *
    def codin(code):
            document = 'fourth.xml'
            xmldoc = parse(document)
            itemlist = xmldoc.getElementsByTagName('item')
            kool =  itemlist[0].attributes['name'].value
            print kool
            if code ==  kool:
                    print type(kool)
                    print type(code)
                    print "found"
            else:
                    print "not found"
    
    
            for s in itemlist :
                    if code in s.attributes['name'].value:
                            print "Country code matched "
                            country = s.firstChild.nodeValue
                            print country
                            print type(country)
                    else:
                            print "not found"
    
    codin('001')
    

XML data:

    
    
    <data>
        <items>
            <item name="001">India</item>
            <item name="002">China</item>
            <item name="003">Spain</item>
            <item name="004">Pakistan</item>
        </items>
    </data>
    


Answer: It's not clear what you are expecting. Following code reads value from xml
file and store it as dictionary so that, you can do your comparison in
dictionary.

    
    
    from  xml.dom.minidom import *
    def codin(code):
        document = 'fourth.xml'
        xmldoc = parse(document)
        items = xmldoc.getElementsByTagName('items')
        kool = ""
        countryKool = {}
        for n in items:
            rv = getChild(n,'item')
            for v in rv:
                country = v.childNodes[0].nodeValue
                attr = v.getAttributeNode('name')
                if attr:
                    kool = attr.nodeValue.strip()
                    print "One of item is " , country, " and attribute is ",kool
                    countryKool[kool] = country
    
        if code in countryKool:
            print "found"
        else:
            print "not found"
    
        print "Mapping of Country and kool ", countryKool #contains mapping for country and kool
    
    
    def getChild(n,v):
        for child in n.childNodes:
            if child.localName==v:
                yield child
    
    codin('001')
    

Output:

    
    
    One of item is  India  and attribute is  001
    One of item is  China  and attribute is  002
    One of item is  Spain  and attribute is  003
    One of item is  Pakistan  and attribute is  004
    found
    Mapping of Country and kool  {u'003': u'Spain', u'002': u'China', u'001': u'India', u'004': u'Pakistan'}
    

