问题
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?
回答1:
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")
回答2:
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.
来源:https://stackoverflow.com/questions/17486882/cross-browser-getelementsbytagname-with-namespace-from-responsexml