XML解析

from xml.dom.minidom import parse, parseString

g_cntTab = 0

def dispNode(param):
    global g_cntTab
    
    nodeList = param.childNodes
    
    for node in nodeList:
        if(node.nodeType == node.ELEMENT_NODE):
            # ELEMENT
            print getTab(g_cntTab) + node.tagName
            # ATTRIBUTE
            if(node.hasAttribute("no")):
                print getTab(g_cntTab) + "no = " + node.getAttribute("no")
            g_cntTab += 1
            dispNode(node)
            g_cntTab -= 1
        elif(node.nodeType == node.TEXT_NODE):
            # TEXT
            if(node.data.strip() != ""):
                print getTab(g_cntTab) + node.data.strip()

def getTab(count):
    return "  " * count
def main():
    dom1 = parse("test.xml") # parse an XML file by name
    
    dispNode(dom1)
            
    dom1.unlink()

main()