Ray
Ray

Reputation: 15

Having an issue with Javascript getElementsByTagName on chrome

I am having an issue with getting xml tag name length in chrome browser. When I try this getElementsByTagName("auth:accountInfo") it is working fine on FF but not working on chrome. If I use getElementsByTagName("accountInfo") it is working on chrome but it is not working on FF. Can some one help me in writing correct logic which works on both browsers? Below is my sample code.

objXHR = XMLHttpRequest()
if(objXHR.status == 200)
        {
            var accountInfo = objXHR.responseXML.getElementsByTagName("auth:accountInfo");
            if (accountInfo.length > 0)
            {
                // do something
                }

Upvotes: 0

Views: 412

Answers (1)

Kaiido
Kaiido

Reputation: 136986

Use getElementsByTagNameNS.

getElementsByTagNameNS(authNS, 'accountInfo');

authNS, being the namespaceURI you've declared with the name auth in your XML doc.

Note that you could achieve the same with XPath query //auth:accountInfo, but you would need to pass an namespaceResolver function to return the correct nameSpaceURI. Also note that this method implies more code, and is slower than other DOM methods.

Finally, you could also achieve this with querySelectorAll('*|accountInfo'), but in this case, all elements with the tagName accountInfo would be picked, whatever their nameSpace.

let start = _ =>{
var doc = new DOMParser().parseFromString(sonnet, 'application/xml');
console.log('getElementsByTagNameNS');
console.log(doc.getElementsByTagNameNS("http://www.authors.com/", 'author')[0].outerHTML);

// Alternatively with XPath
function resolveNS(name) {
  if (name === 'auth') {
    return 'http://www.authors.com/'
  }
}
var query = doc.evaluate("//auth:author", doc, resolveNS, XPathResult.ANY_TYPE, null);
var elements = [];
var el;
while (el = query.iterateNext()) {
  elements.push(el);
}
console.log('XPATH');
console.log(elements[0].outerHTML)

// Or, with querySelectorAll
console.log('querySelectorAll');
console.log(doc.querySelectorAll('*|author')[0].outerHTML);
};

var sonnet = `<sonnet type='Shakespearean'>
  <auth:author xmlns:auth="http://www.authors.com/">
    <last-name>Shakespeare</last-name>
    <first-name>William</first-name>
    <nationality>British</nationality>
    <year-of-birth>1564</year-of-birth>
    <year-of-death>1616</year-of-death>
  </auth:author>
  <!-- Is there an official title for this sonnet? They're sometimes named after the first line. -->
  <title>Sonnet 130</title>
  <lines>
    <line>My mistress' eyes are nothing like the sun,</line>
    <line>Coral is far more red than her lips red.</line>
    <line>If snow be white, why then her breasts are dun,</line>
    <line>If hairs be wires, black wires grow on her head.</line>
    <line>I have seen roses damasked, red and white,</line>
    <line>But no such roses see I in her cheeks.</line>
    <line>And in some perfumes is there more delight</line>
    <line>Than in the breath that from my ...</line>
  </lines>
</sonnet>`;

start();

Upvotes: 1

Related Questions