3

How can I use the selectNode() function on a parsed XML message in javascript?

<script>
var xmlStr = "<tagName>some text here</tagName>";
var xmlDoc = (new DOMParser()).parseFromString(xmlStr, "text/xml");
var nodes = xmlDoc.selectNodes('tagName');
</script>

I get error for this code: Uncaught TypeError: xmlDoc.selectNodes is not a function

nagy.zsolt.hun
  • 6,292
  • 12
  • 56
  • 95

2 Answers2

4

Actually XmlDocument.SelectNodes method exists, but in the .NET Framework.

In JavaScript, you can use the getElementsByTagName() method

var nodes = xmlDoc.getElementsByTagName("tagName")[0];
  • @user6186801 you probably mean the code without the [0]. I.e., the nodes variable is an array, not it's first element. right? – primehunter Feb 10 '21 at 14:03
0

selectNode() is not on javascript, you meant that you are looking for equivalent function?

nodes.getElementsByTagName("tagname");
The scion
  • 1,001
  • 9
  • 19