Using XPath in JavaScript (Mozilla based)
doc (xml document)
<codes> <codex value='bQ' index='Q'/> <codex value='S' index='R'/> <codex value='PU' index='S'/> <codex value='ji' index='T'/> <codex value='0' index='U'/> <codex value='33' index='V'/> <codex value='A' index='W'/> <codex value='tO' index='X'/> <codex value='fW' index='Y'/> <codex value='P' index='Z'/> <codex value='4h' index='a'/> <codex value='B' index='b'/> <codex value='m' index='c'/> <codex value='qf' index='d'/> <codex value='uJ' index='e'/> </codes>
Assuming the doc object below contains the XML data from above.
var nodesSnapshot = doc.evaluate("codes/codex[@index='a']", doc, null, XPathResult. UNORDERED_NODE_SNAPSHOT_TYPE, null ); node = nodesSnapshot.snapshotItem(0); msg = "The secret code for '" + node.getAttribute('index') + "' is " + node.getAttribute('value'); alert(msg);
output (value from the alert box)
"The secret code for 'a' is 4h"
see also: Reading an XML file using JavaScript