0

The code below takes an XML File and puts it into an HTML table. I need this table to only contain students whose grade was A but am not sure how to filter this so it currently just grabs all of the student's names, ages and grades regardless of the grade they achieved. Could someone please show me how to only add students who achieved an A to the table?

HTML FILE

<html>
<head>
<style>
table, th, td {
  border: 1px solid black;
  border-collapse:collapse;
}
th, td {
  padding: 5px;
}
</style>
</head>
<body>
    
<table id="Advanced Web Development"></table>

<script>
function loadXMLDoc() {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      myFunction(this);
    }
  };
  xmlhttp.open("GET", "Dom_file2.xml", true);
  xmlhttp.send();
}
function myFunction(xml) {
  var i;
  var xmlDoc = xml.responseXML;
  var table="<tr><th>NAME</th><th>AGE</th><th>GRADE</th></tr>";
  var x = xmlDoc.getElementsByTagName("STUDENT");
  for (i = 0; i <x.length; i++) {
    table += "<tr><td>" +
    x[i].getElementsByTagName("NAME")[0].childNodes[0].nodeValue +
    "</td><td>" +
    x[i].getElementsByTagName("AGE")[0].childNodes[0].nodeValue +
    "</td><td>" +
    x[i].getElementsByTagName("GRADE")[0].childNodes[0].nodeValue +
    "</td></tr>";
  }
  document.getElementById("Advanced Web Development").innerHTML = table;
}
loadXMLDoc()
</script>

</body>
</html>

XML FILE

<CLASS ID="Advanced Web Development">
    <STUDENT>
        <NAME>Tom</NAME>
        <AGE>19</AGE>
        <HEIGHT>1.3</HEIGHT>
        <SEX>M</SEX>
        <GRADE>B</GRADE>
    </STUDENT>
    <STUDENT>
        <NAME>Dick</NAME>
        <AGE>29</AGE>
        <HEIGHT>1.1</HEIGHT>
        <SEX>M</SEX>
        <GRADE>A</GRADE>
    </STUDENT>
    <STUDENT>
        <NAME>Harry</NAME>
        <AGE>39</AGE>
        <HEIGHT>1.5</HEIGHT>
        <SEX>M</SEX>
        <GRADE>C</GRADE>
    </STUDENT>
    <STUDENT>
        <NAME>Mary</NAME>
        <AGE>30</AGE>
        <HEIGHT>1.1</HEIGHT>
        <SEX>F</SEX>
        <GRADE>A</GRADE>
    </STUDENT>
    <STUDENT>
        <NAME>Tim</NAME>
        <AGE>23</AGE>
        <HEIGHT>1.5</HEIGHT>
        <SEX>M</SEX>
        <GRADE>A</GRADE>
    </STUDENT>
</CLASS>

1 Answer 1

1
if (x[i].getElementsByTagName("GRADE")[0].childNodes[0].nodeValue === 'A') {
   // add it to your output
   table += ...
}

Just write an if statement to only do the logic if the grade is A.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.