Why is it that when I insert the parent node, nothing shows up -- but when I use the root node ("dataroot"), only the first child node displays in the table even though there are multiple child/sibling nodes? Here's the HTML code:
<!DOCTYPE html>
<html>
<head>
<title>
Pharma-Find
</title>
<link rel="stylesheet" type="text/css" href="getDrugs.css">
<script type = "text/javascript" src="getDrugs.js"></script>
</head>
<body>
<button type="button" onclick="loadXMLDoc()">Pharma Find</button>
<br><br>
<table id="demo"></table>
</body>
</html>
This is the JS script:
function loadXMLDoc() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xmlhttp.open("GET", "drugA.xml", true);
xmlhttp.send();
}
function myFunction(xml) {
var i;
var xmlDoc = xml.responseXML;
var table="<tr><th>Generic Brand</th><th>Brand Name</th><th>lnk</th><th>purpose</th><th>DEASch</th><th>Category</th><th>Study Topic</th></tr>";
var x = xmlDoc.getElementsByTagName("dataroot");
for (i = 0; i <x.length; i++) {
table +=
"<tr><td>" +
x[i].getElementsByTagName("GenericName")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("BrandName")[0].childNodes[0].nodeValue +
"</td><td>"+
x[i].getElementsByTagName("lnk")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("Purpose")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("DEASch")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("Category")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("StudyTopic")[0].childNodes[0].nodeValue + "</td></tr>";
}
document.getElementById("demo").innerHTML = table;
}