Aaron
Aaron

Reputation: 3659

Missing XML node

I have the following XML file:

<?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>

And using the following 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>

This is working fine, but if one of the values in the XML file has no value such as <from></from> then the rest of the values will not return any data.

Any ideas how I can detect this and move on, even if there is no value in the node?

Upvotes: 2

Views: 2487

Answers (3)

Bergi
Bergi

Reputation: 664971

Just detect it:

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

Upvotes: 0

Joseph
Joseph

Reputation: 119867

It's because when the tag has no value, it has no child node, specifically a text node. This makes childNode contain nothing, and childNode[0] is undefined. If you try to get a property of an undefined, it will run into an error:

//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 using a blank <span>

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270677

If childNodes[0] doesn't exist on any of these, you'll get an error when trying to call nodeValue on it. Test if it exists first:

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

Upvotes: 1

Related Questions