For a program in Python I am looking for a way to find a specific text in an element of XML and to find out which node number it is.
This is the xml:
-<shortcut>
<label>33060</label>
<label2>Common Shortcut</label2>
</shortcut>
-<shortcut>
<label>Test</label>
</shortcut>
Of course I know it is probably node number 2 in here, but the xml file can be longer.
This are to ways I tried it, but I don't get it to work properly:
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"