缺少XML节点

时间:2012-05-25 01:38:02

标签: javascript html xml

我有以下XML文件:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!-- Edited by XMLSpy® -->
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>

    <body>Don't forget me this weekend!</body>
</note>

使用以下Javascript / HTML:

<html>

    <body>
        <h1>W3Schools Internal Note</h1>
        <div>
            <b>To:</b>
            <span id="to"></span>
            <br />
            <b>From:</b>
            <span id="from"></span>
            <br />
            <b>Message:</b>
            <span id="message"></span>
        </div>
        <script type="text/javascript">
            if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else { // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.open("GET", "note.xml", false);
            xmlhttp.send();
            xmlDoc = xmlhttp.responseXML;
            document.getElementById("to").innerHTML = xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
            document.getElementById("from").innerHTML = xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
            document.getElementById("message").innerHTML = xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
        </script>
    </body>

</html>

这样可以正常工作,但如果XML文件中的某个值没有<from></from>之类的值,则其余值将不会返回任何数据。

如果节点中没有值,我会如何检测到并继续前进?

3 个答案:

答案 0 :(得分:1)

如果其中任何一个都不存在childNodes[0],则在尝试呼叫nodeValue时会出错。先测试它是否存在:

// Get and store the nodes from the xml first
var toNode = xmlDoc.getElementsByTagName("to")[0];
var fromNode = xmlDoc.getElementsByTagName("from")[0];
var messageNode = xmlDoc.getElementsByTagName("message")[0];

// And only assign them if they actually have childNodes. Otherwise use an empty string
document.getElementById("to").innerHTML = toNode.childNodes.length ? toNode.childNodes[0].nodeValue : "";
document.getElementById("from").innerHTML = fromNode.childNodes.length ? fromNode.childNodes[0].nodeValue : "";
document.getElementById("message").innerHTML = messageNode.childNodes.length ? messageNode.childNodes[0].nodeValue : "";

答案 1 :(得分:0)

这是因为当标签没有值时,它没有子节点,特别是文本节点。这使childNode不包含任何内容,childNode[0]undefined。如果您尝试获取undefined的属性,则会遇到错误:

//gets the from
xmlDoc.getElementsByTagName("from")[0]

//this is undefined since there is no childNode
xmlDoc.getElementsByTagName("from")[0].childNodes[0]

//getting a property of undefined will cause an error and kill execution
xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;

Here's a sample使用空白<span>

答案 2 :(得分:0)

只是检测它:

var map = {to:"to", from:"from", message:"body"};
for (var id in map) {
    var node = xmlDoc.getElementsByTagName(map[id]])[0]; // worst case: accesses first node of empty NodeList
    if (node && node.childNodes.length) // >0
         document.getElementById(id).innerText = node.childNodes[0].nodeValue;
}