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?

Answers:

Answer

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")
Answer

If you don't care about the namespace then you could use the following:

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

This will fetch any element with the desired name regardless of which namespace it has or whether it has any namespace at all. Additionally, this should work as expected across different browsers.

See link for documentation.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.