Я получаю некоторые странные результаты при синтаксическом анализе XML в JavaScript. У меня есть следующий XML во внешнем файле:
<?xml version="1.0" encoding="UTF-8" ?>
<alerts>
<alert><![CDATA[ This is the first alert message in a series... ]]></alert>
<alert><![CDATA[ This is the second alert message in a series, which features a <a href="http://www.facebook.com" target="blank">hyperlink</a>... ]]></alert>
<alert><![CDATA[ This is the third alert message in a series, which features <span class="emphasizedAlertText">text formatted via a css rule</span> ]]></alert>
<alert><![CDATA[ This is the fourth alert message in a series, which features a <span class="fauxHyperlink" onclick="someFunction();">javascript call</span>... ]]></alert>
</alerts>
После получения файла через XMLHttpRequest у меня есть следующая функция для вывода его на страницу:
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 < xhrTree.length; i++)
{
if(xhrTree[i].nodeName != '#text')
{
rslt.innerHTML = rslt.innerHTML + '<br/>' + xhrTree[i].nodeName + ' (' + xhrTree[i].nodeType + ') ' + ' = ' + xhrTree[i].childNodes[0].nodeValue;
}
}
//rslt.innerHTML = outMsg;
}
}
else
{
xhrRsp = 'Loading...';
}
}
Когда я запускаю скрипт в браузере, я получаю следующее:
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...
Я озадачен тем, почему число узлов возвращается как 9, и почему каждый второй из них имеет вид #text, который я сейчас отфильтрую в приведенном выше сценарии. Я искал вокруг и не могу понять, что мне не хватает.
Заранее спасибо!