Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cross-browser 'getElementsByTagName' with namespace from responseXML

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
}

Above code works fine in Firefox and IE but in chrome it throws null.. i mean it does not get any data.. when i alert the rows.length it gives me 0 always in chrome.

Then i searched in google and understood the issue is with xsd:element, then i changed "z:row" to only "row". Then it worked in Chrome but Firefox and IE returned 0 for rows.length.

Is there any method which across all browsers?

like image 436
KrankyCode Avatar asked Jul 05 '13 10:07

KrankyCode


1 Answers

This is what I use:

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

With in your case:

byTagNS(responseXML, "row", "z")
like image 150
Christophe Avatar answered Oct 04 '22 19:10

Christophe