Cross-browser 'getElementsByTagName' with namespace from responseXML

I am trying to read an XML response using getElementsByTagName :

 var rows = items.responseXML.getElementsByTagName("z:row"); for (var i=0; i<rows.length; i++) { //do something } 

The code works fine in Firefox and IE, but in chrome it throws null .. I mean that it does not receive any data. When I alert rows.length , it gives me 0 always in chrome.

Then I searched on google and realized that the problem was with xsd:element , and then I changed "z:row" only to "row" . It then worked in Chrome, but Firefox and IE returned 0 for rows.length .

Is there any method that is in all browsers?

+6
source share
2 answers

This is what I use:

 function byTagNS(xml,tag,ns) { return xml.getElementsByTagNameNS ? xml.getElementsByTagNameNS(ns,tag) : xml.getElementsByTagName(ns+":"+tag); } 

In your case:

 byTagNS(responseXML, "row", "z") 
+2
source

If you don't like the namespace, you can use the following:

 xml.getElementsByTagNameNS("*", "yourElementHere") 

This will result in the selection of any element with the desired name no matter what namespace it has or has any namespace at all. Also, this should work as expected in different browsers.

See the link for documentation .

0
source

Source: https://habr.com/ru/post/948773/


All Articles