I have this code to parse XML. How would I address an empty node in the XML? At present no output is delivered at all with the "v2" node being empty...
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "sample.xml", true);
xhttp.send();
}
function myFunction(xml) {
var i;
var xmlDoc = xml.responseXML;
var table="<tr><th>c1</th><th>c2</th></tr>";
var x = xmlDoc.getElementsByTagName("product");
for (i = 0; i <x.length; i++) {
table += "<tr><td>" +
x[i].getElementsByTagName("v1")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("v2")[0].childNodes[0].nodeValue +
"</td></tr>";
}
document.getElementById("demo").innerHTML = table;
}
</script>
XML:
<products>
<product>
<v1>some data</v1>
<v2/>
</product>
</products>