How to get all child and sibling data from an XML file and output to a table

16 Views Asked by At

I have a XML file that contains a record "Parent" Keyword "child" HistoryRecord "child" & Checkdate,Postion,Delta, & URLFound "siblings". I would like to out put the data into a table with each HistoryRecord data populating a new column in the Keywords row.

This code is able to ghet the first HistoryRecord but not any addtional.

<html>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    
    <script>
$(document).ready(function(){
  $("button").click(function(){
    $("table").toggleClass("open");
  });
});
</script>
<style>
table,th,td {
  border : 1px solid black;
  border-collapse: collapse;
}
th,td {
  padding: 5px;
}
    .open{display:none;}
</style>
<body>

<h2></h2>

<button type="button" onclick="loadDoc()">Keyword Ranking Report</button>
<br><br>
<table class="closed" id="demo"></table>

<script>
function loadDoc() {
  const xhttp = new XMLHttpRequest();
  xhttp.onload = function() {
    myFunction(this);
  }
  xhttp.open("GET", "gsep.xml");
  xhttp.send();
}
function myFunction(xml) {
  const xmlDoc = xml.responseXML;
  const x = xmlDoc.getElementsByTagName("record");
  let table="<tr><th>Keyword</th><th>Position</th><th>Date</th><th>Past Position</th><th>Past Date</tr>";
  for (let i = 0; i <x.length; i++) { 
    table += "<tr><td>" +
    x[i].getElementsByTagName("Keyword")[0].childNodes[0].nodeValue +
    "</td><td>" +
        x[i].getElementsByTagName("Position")[0].childNodes[0].nodeValue +
    "</td><td>" +
    x[i].getElementsByTagName("CheckDate")[0].childNodes[0].nodeValue +
    "</td></tr>";
      
  }
  document.getElementById("demo").innerHTML = table;
}
    </script>
    
</body>
</html>
'''
My xml looks like this
'''
        <record>
            <Keyword>doctorate in clinical psychology</Keyword>
            <SearchEngine>Google</SearchEngine>
            <HistoryRecord>
                <CheckDate>Mar 19, 2024</CheckDate>
                <Position>2</Position>
                <Delta>0</Delta>
                <URLFound>https://gsep.pepperdine.edu/doctorate-clinical-psychology/</URLFound>
            </HistoryRecord>
            <HistoryRecord>
                <CheckDate>Nov 14, 2023</CheckDate>
                <Position>2</Position>
                <Delta></Delta>
                <URLFound>https://gsep.pepperdine.edu/doctorate-clinical-psychology/</URLFound>
            </HistoryRecord>
            </record>           
<record>
            <Keyword>masters of education</Keyword>
            <SearchEngine>Google</SearchEngine>
            <HistoryRecord>
                <CheckDate>Mar 19, 2024</CheckDate>
                <Position>46</Position>
                <Delta>Entered</Delta>
                <URLFound>https://gsep.pepperdine.edu/degrees-programs/masters-education.htm</URLFound>
            </HistoryRecord>
            <HistoryRecord>
                <CheckDate>Mar 17, 2024</CheckDate>
                <Position>Not in top 50</Position>
                <Delta>Dropped</Delta>
                <URLFound></URLFound>
            </HistoryRecord>
            <HistoryRecord>
                <CheckDate>Mar 14, 2024</CheckDate>
                <Position>43</Position>
                <Delta>Entered</Delta>
                <URLFound>https://gsep.pepperdine.edu/degrees-programs/masters-education.htm</URLFound>
            </HistoryRecord>
            <HistoryRecord>
                <CheckDate>Mar 10, 2024</CheckDate>
                <Position>Not in top 50</Position>
                <Delta>Stays out</Delta>
                <URLFound></URLFound>
            </HistoryRecord>
            <HistoryRecord>
                <CheckDate>Mar 3, 2024</CheckDate>
                <Position>Not in top 50</Position>
                <Delta>Stays out</Delta>
                <URLFound></URLFound>
            </HistoryRecord>
            <HistoryRecord>
                <CheckDate>Feb 28, 2024</CheckDate>
                <Position>Not in top 50</Position>
                <Delta>Dropped</Delta>
                <URLFound></URLFound>
            </HistoryRecord>
            <HistoryRecord>
                <CheckDate>Feb 18, 2024</CheckDate>
                <Position>9</Position>
                <Delta>Entered</Delta>
                <URLFound>https://gsep.pepperdine.edu/degrees-programs/masters-education.htm</URLFound>
            </HistoryRecord>
            <HistoryRecord>
                <CheckDate>Feb 4, 2024</CheckDate>
                <Position>Not in top 50</Position>
                <Delta>Stays out</Delta>
                <URLFound></URLFound>
            </HistoryRecord>
            <HistoryRecord>
                <CheckDate>Jan 21, 2024</CheckDate>
                <Position>Not in top 50</Position>
                <Delta>Stays out</Delta>
                <URLFound></URLFound>
            </HistoryRecord>
            <HistoryRecord>
                <CheckDate>Jan 14, 2024</CheckDate>
                <Position>Not in top 50</Position>
                <Delta>Dropped</Delta>
                <URLFound></URLFound>
            </HistoryRecord>
            <HistoryRecord>
                <CheckDate>Oct 30, 2023</CheckDate>
                <Position>76</Position>
                <Delta></Delta>
                <URLFound>https://gsep.pepperdine.edu/degrees-programs/masters-education.htm</URLFound>
            </HistoryRecord>
            </record>
0

There are 0 best solutions below