English 中文(简体)
缺少 XML 节点
原标题:Missing XML node

我有以下 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 文件中的一个值没有值, 如 , 那么其余值将不会返回任何数据 。

即使节点没有价值 我怎样才能发现它 继续前进呢?

最佳回答

s 因为当标签没有值时, 它没有子节点, 特别是文本节点。 这使得 < code> childNode 没有包含任何内容, 而 childNode[0] 无法定义的 。 如果您试图获取一个 未定义 的属性, 它会遇到错误 :

//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;

< a href=" "http://jsfiddle.net/ utTHLk/" rel=“ no follow" > 这里使用空白 的样本 。

问题回答

如果 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 : "";

只需检测到它:

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;
}




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签