Search code examples
pythonxmlminidom

Parsing XML in python using minidom


I have an XML as under;

<root>
 <entry>
    <accession>A</accession>
    <accession>B</accession>
    <accession>C</accession>
    <feature type="cross-link" description="sumo2">
        <location>
            <position position="15111992"/>
        </location>
    </feature>
    <feature type="temp" description="blah blah sumo">
        <location>
            <position position="12345"/>
        </location>
    </feature>
</entry>
<entry>
  <accession>X</accession>
    <accession>Y</accession>
    <accession>Z</accession>
    <feature type="test" description="testing">
        <location>
            <position position="1"/>
        </location>
    </feature>
    <feature type="cross-link" description="sumo hello">
        <location>
            <position position="11223344"/>
        </location>
    </feature>
 </entry>
</root>

I need to fetch the value of posiiton attribute whose feature type is "cross-link" and description contains the word sumo. This is what I have tried so far which correctly gives me those value whose feature type is "cross-link" and description contains the word sumo.

from xml.dom import minidom
xmldoc = minidom.parse('P38398.xml')
itemlist = xmldoc.getElementsByTagName('feature')

for s in itemlist:
    feattype = s.attributes['type'].value
    description = s.attributes['description'].value
    if "SUMO" in description:
        if "cross-link" in feattype:
            print feattype+","+description

How can I extract the value of position once I have the feature type as "cross-link" and description containing the word "sumo"?


Solution

  • You are nearly there except two points:

    • You have to change your "sumo" search pattern to lowercase to match the data given above
    • You then need to add something like the following to your loop body

      posList = s.getElementsByTagName('position')
      for p in posList:
          print "-- position is {}".format(p.attributes['position'].value)