■ XML 노드를 순회하는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import xml.dom.minidom def GetNodeTypeName(nodeType): if nodeType == 1: return "ELEMENT" elif nodeType == 2: return "ATTRIBUTE" elif nodeType == 3: return "TEXT" elif nodeType == 4: return "CDATA SECTION" elif nodeType == 5: return "ENTITY" elif nodeType == 6: return "PROCESSING INSTRUCTION" elif nodeType == 7: return "COMMENT" elif nodeType == 8: return "DOCUMENT" elif nodeType == 9: return "DOCUMENT TYPE" else: return "NOTATION" def PrintNode(parentNode, level): if parentNode.nodeType == 3: return if level == 0: indent = "" else: indent = " " * level * 4 print("{0}{1} ({2})".format(indent, parentNode.nodeName, GetNodeTypeName(parentNode.nodeType))) for childNode in parentNode.childNodes: if childNode.nodeType == 3: continue PrintNode(childNode, level + 1) document = xml.dom.minidom.parse("book.xml") for childNode in document.childNodes: PrintNode(childNode, 0) |