对于 Python 中的程序,我正在寻找一种方法来查找 XML 元素中的特定文本并找出它是哪个节点号。
这是xml:
-<shortcut>
<label>33060</label>
<label2>Common Shortcut</label2>
</shortcut>
-<shortcut>
<label>Test</label>
</shortcut>
当然我知道这里可能是节点号 2,但是 xml 文件可以更长。
这是我尝试过的方法,但我没有让它正常工作:
xmldoc = minidom.parse("/DATA.xml")
Shortcut = xmldoc.getElementsByTagName("shortcut")
Label = xmldoc.getElementsByTagName("label")
print xmldoc.getElementsByTagName("label")[12].firstChild.nodeValue (works)
for element in Label:
if element.getAttributeNode("label") == 'Test':
# if element.getAttributeNode('label') == "Test":
print "element found"
else:
print "element not found"
for node in xmldoc.getElementsByTagName("label"):
if node.nodeValue == "Test":
print "element found"
else:
print "element not found"