我正在製作一個解析XML文件的Python程序。我需要迭代NodeList,但是我有一個問題,用「NodeList中的節點」語法來完成。Python/Minidom:迭代NodeList
下面是代碼的示例:
docToInclude = parse(node.getAttribute("file"))
print ("childNode count : " , len(docToInclude.documentElement.childNodes))
print ("childNodes : " , docToInclude.documentElement.childNodes)
print("")
for i in range(0, len(docToInclude.documentElement.childNodes)):
print ("i = ", i , "nodeName = " + docToInclude.documentElement.childNodes[i].nodeName)
print("")
for elementNode in docToInclude.documentElement.childNodes :
print ("node name : " , elementNode.nodeName)
node.parentNode.insertBefore(elementNode, insertPosition)
這裏是輸出:
childNode count : 3
childNodes : [<DOM Text node "'\n\n\t'">, <DOM Element: messageList at 0x3a4e570>, <DOM Text node "'\n\n'">]
i = 0 nodeName = #text
i = 1 nodeName = messageList
i = 2 nodeName = #text
node name : #text
node name : #text
如果我與在節點列表語法節點,一個元件被跳過迭代。 你對這個問題的起源有什麼想法嗎?