Search code examples
javascriptxmlgetelementsbytagname

getElementsByTagName stopping at triangle bracket


I am using getElementsByTagName in this context

 TheTitle = xmlDoc.getElementsByTagName("ArticleTitle")[i].childNodes[0].nodeValue;

to obtain the node value but when the text includes triangle brackets such as:

<ArticleTitle>"The Cat Sat on The <i>Mat</i>"</ArticleTitle>

I am only able to retrieve

The Cat Sat on The

How can I prevent triangle brackets in the node text from prematurely ending text capture?


Solution

  • <ArticleTitle>"The Cat Sat on The <i>Mat</i>"</ArticleTitle> has three child nodes

    1. text node: The Cat Sat on The
    2. <i> node with <i>Mat</i>
    3. text node "

    So, .childNodes[0].nodeValue; will, of course, be just The Cat Sat on The

    To fix, use:

    TheTitle = xmlDoc.getElementsByTagName("ArticleTitle")[i].textContent;
    

    instead

    let doc = `<xml><ArticleTitle>"The Cat Sat on The <i>Mat</i>"</ArticleTitle></xml>`;
    let xmlDoc = new DOMParser().parseFromString(doc, 'text/xml');
    let TheTitle = xmlDoc.getElementsByTagName("ArticleTitle")[0].textContent;
    console.log(TheTitle);