Further Examples of Reading XML

Some further examples of different approaches to reading an XML file. These examples use a different XML file (mut.xml) which is a simplified version of the file you need to parse.

Using .item(0)

#!/usr/bin/env python
from xml.dom import minidom

doc = minidom.parse("mut.xml")

# doc.getElementsByTagName returns NodeList
name = doc.getElementsByTagName("name")[0]
print(name.firstChild.data)

for mutation in doc.getElementsByTagName("mutation"):
        id = mutation.getAttribute("id")
        resnum = mutation.getElementsByTagName("resnum").item(0)
        substitution = mutation.getElementsByTagName("substitution").item(0)
        print("id: %s, resnum: %s, substitution: %s" %
              (id, resnum.firstChild.data, substitution.firstChild.data))

[Code] [XML]

Using the results as lists

#!/usr/bin/env python
from xml.dom import minidom

doc = minidom.parse("mut.xml")

# doc.getElementsByTagName returns NodeList
name = doc.getElementsByTagName("name")[0]
print(name.firstChild.data)

for mutation in doc.getElementsByTagName("mutation"):
        id = mutation.getAttribute("id")
        resnum = mutation.getElementsByTagName("resnum")[0]
        substitution = mutation.getElementsByTagName("substitution")[0]
        print("id: %s, resnum: %s, substitution: %s" %
              (id, resnum.firstChild.data, substitution.firstChild.data))

[Code] [XML]

Using lists and extracting data early

#!/usr/bin/env python
from xml.dom import minidom

doc = minidom.parse("mut.xml")

# doc.getElementsByTagName returns NodeList
name = doc.getElementsByTagName("name")[0]
print(name.firstChild.data)

for mutation in doc.getElementsByTagName("mutation"):
        id = mutation.getAttribute("id")
        resnum = mutation.getElementsByTagName("resnum")[0].firstChild.data
        substitution = mutation.getElementsByTagName("substitution")[0].firstChild.data
        print("id: %s, resnum: %s, substitution: %s" %
              (id, resnum, substitution))

[Code] [XML]

Using loops

#!/usr/bin/env python
from xml.dom import minidom

doc = minidom.parse("mut.xml")

for mutantsNode in doc.getElementsByTagName('mutants'):
        name = mutantsNode.getElementsByTagName("name")[0].firstChild.data
        print(name)

        for mutationNode in mutantsNode.getElementsByTagName("mutation"):
                id = mutationNode.getAttribute("id")
                for resnumNode in mutationNode.getElementsByTagName("resnum"):
                        resnum = resnumNode.firstChild.data
                for substitutionNode in mutationNode.getElementsByTagName("substitution"):
                        substitution = substitutionNode.firstChild.data
                print("id: %s, resnum: %s, substitution: %s" %
                      (id, resnum, substitution))

[Code] [XML]

Continue