Javascript Data Type How to - Use xpath with dom document








Question

We would like to know how to use xpath with dom document.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!-- w  ww.j a  va 2  s .  co  m-->
var xmlString = '<form><name>test</name></form>';
var doc = new DOMParser().parseFromString(xmlString, 'text/xml');
var result = doc.evaluate('/form/name', doc, null, XPathResult.STRING_TYPE, null);
console.log(result.stringValue);

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: