Mega Code Archive

 
Categories / Python Tutorial / XML
 

Accessing Child Nodes

from xml.dom  import minidom xmldoc = minidom.parse('emails.xml') cNodes = xmldoc.childNodes print cNodes[0].toxml() nList = cNodes[1].getElementsByTagName("to") for node in nList:     eList = node.getElementsByTagName("addr")     for e in eList:         print e.toxml() nList = cNodes[1].getElementsByTagName("from") for node in nList:     eList = node.getElementsByTagName("addr")     for e in eList:         print e.toxml() def printNodes (nList, level):     for node in nList:         print ("  ")*level, node.nodeName, node.nodeValue         printNodes(node.childNodes, level+1) printNodes(xmldoc.childNodes, 0)