Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript: childNodes.length showing too many elements?
    primarykey
    data
    text
    <p>I'm getting some weird results while parsing some XML in JavaScript. I have the following XML in an external file:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;alerts&gt; &lt;alert&gt;&lt;![CDATA[ This is the first alert message in a series... ]]&gt;&lt;/alert&gt; &lt;alert&gt;&lt;![CDATA[ This is the second alert message in a series, which features a &lt;a href="http://www.facebook.com" target="blank"&gt;hyperlink&lt;/a&gt;... ]]&gt;&lt;/alert&gt; &lt;alert&gt;&lt;![CDATA[ This is the third alert message in a series, which features &lt;span class="emphasizedAlertText"&gt;text formatted via a css rule&lt;/span&gt; ]]&gt;&lt;/alert&gt; &lt;alert&gt;&lt;![CDATA[ This is the fourth alert message in a series, which features a &lt;span class="fauxHyperlink" onclick="someFunction();"&gt;javascript call&lt;/span&gt;... ]]&gt;&lt;/alert&gt; &lt;/alerts&gt; </code></pre> <p>After grabbing the file via XMLHttpRequest, I have the following function to output it to the page:</p> <pre><code>function testFunc() { var xhrRsp = 'failed to initialize'; rslt = document.getElementById('rslt'); if(xhr.readyState == 4) { if(xhr.status == 200) { xhrRsp = xhr.responseXML; xhrTree = xhrRsp.documentElement.childNodes; rslt.innerHTML = xhrRsp + ' (' + xhrTree.length + ' nodes) '; for(var i = 0; i &lt; xhrTree.length; i++) { if(xhrTree[i].nodeName != '#text') { rslt.innerHTML = rslt.innerHTML + '&lt;br/&gt;' + xhrTree[i].nodeName + ' (' + xhrTree[i].nodeType + ') ' + ' = ' + xhrTree[i].childNodes[0].nodeValue; } } //rslt.innerHTML = outMsg; } } else { xhrRsp = 'Loading...'; } } </code></pre> <p>When I run the script in the browser, I get this as the result:</p> <pre><code>Click Me [object Document] (9 nodes) alert (1) = This is the first alert message in a series... alert (1) = This is the second alert message in a series, which features a hyperlink... alert (1) = This is the third alert message in a series, which features text formatted via a css rule alert (1) = This is the fourth alert message in a series, which features a javascript call... </code></pre> <p>I'm puzzled as to why the number of nodes comes back as 9, and why every other one of these comes in as #text, which I'm currently filtering out in the above script. I've been looking around and can't figure out what I'm missing.</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload