简体   繁体   English

如何将数据从 XML 文件导入 HTML 表格

[英]How to import data from XML file to HTML table

I got an XML file which looks like this.我得到了一个看起来像这样的 XML 文件。 I want to make an HTML table that will contain this data in order.我想制作一个按顺序包含这些数据的 HTML 表格。 Any idea how?知道怎么做吗? I have very little knowledge with javascript.我对javascript知之甚少。 Now when I click on the button:现在当我点击按钮时:

I get an error:我收到一个错误:

javascript.js:16 Uncaught TypeError: Cannot read property 'getElementsByTagName' of null javascript.js:16 未捕获的类型错误:无法读取 null 的属性“getElementsByTagName”

function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      myFunction(this);
    }
  };
  xhttp.open("GET", "examData.xml", true);
  http.send();
}
function myFunction(xml){
  var i;
  var xmlDoc = xml.responseXML;
  var table="<tr><th>Date</th><th>Type of exam</th><th>Course</th><th>Semester</th><th>Students</th><th>From of exam</th><th>Room</th></tr>";
  var x = xmlDoc.getElementsByTagName("exam");
  for (i = 0; i <x.length; i++) { 
    table += "<tr><td>" +
    x[i].getElementsByTagName("date")[0].childNodes[0].nodeValue + "</td><td>" +
    x[i].getElementsByTagName("type")[0].childNodes[0].nodeValue + "</td></tr>";
    x[i].getElementsByTagName("course")[0].childNodes[0].nodeValue + "</td></tr>";
    x[i].getElementsByTagName("semester")[0].childNodes[0].nodeValue + "</td></tr>";
    x[i].getElementsByTagName("students")[0].childNodes[0].nodeValue + "</td></tr>";
    x[i].getElementsByTagName("form")[0].childNodes[0].nodeValue + "</td></tr>";
    x[i].getElementsByTagName("room")[0].childNodes[0].nodeValue + "</td></tr>";
  }
  document.getElementById("demo").innerHTML = table;
}
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<exams>
  <exam>
    <date>2020-01-15</date><type>Written</type><course>SJD-1Z</course><semester>1</semester><students>0</students><form>Ordinary</form><room>DSADSADSA</room>
  </exam>
  <exam>
    <date>2020-01-15</date><type>Written</type><course>SJD-1Z</course><semester>1</semester><students>0</students><form>Ordinary</form><room>DSADSADSA</room>
  </exam>
  <exam>
    <date>2020-01-15</date><type>Written</type><course>SJD-1Z</course><semester>1</semester><students>0</students><form>Ordinary</form><room>DSADSADSA</room>
  </exam>
  <exam>
    <date>2020-01-15</date><type>Written</type><course>SJD-1Z</course><semester>1</semester><students>0</students><form>Ordinary</form><room>DSADSADSA</room>
  </exam>
  <exam>
    <date>2020-01-15</date><type>Written</type><course>SJD-1Z</course><semester>1</semester><students>0</students><form>Ordinary</form><room>DSADSADSA</room>
  </exam>
  <exam>
    <date>2020-01-15</date><type>Written</type><course>SJD-1Z</course><semester>1</semester><students>0</students><form>Ordinary</form><room>DSADSADSA</room>
  </exam>
  <exam>
    <date>2020-01-15</date><type>Written</type><course>SJD-1Z</course><semester>1</semester><students>0</students><form>Ordinary</form><room>DSADSADSA</room>
  </exam>
  <exam>
    <date>2020-01-15</date><type>Written</type><course>SJD-1Z</course><semester>1</semester><students>0</students><form>Ordinary</form><room>DSADSADSA</room>
  </exam>
</exams>
<h1>Our first project</h1>
<button type="button" onclick="loadDoc()">Get Table</button>
<br><br>
<table id="demo"></table> 

you have to use AJAX:你必须使用 AJAX:

something like that:类似的东西:

const myTable   = document.getElementById('demo')
    , myXMLfile = 'examData.xml';
var xmlhttp     = new XMLHttpRequest();

xmlhttp.onreadystatechange = function() {
  if (4 === this.readyState) {  /* DONE */
    if (200 === this.status) {  /* LOADING */
      fctGetXML(this);
    }
    else {
      console.warn("error %d on loading file: %s !", this.status, myXMLfile);
    }
  }
};


function loadDoc() {
  xmlhttp.open("GET", myXMLfile, true);
  xmlhttp.send();
}

function fctGetXML(xml) {
  let xmlDoc = xml.responseXML;

  xmlDoc.querySelectorAll('exam').forEach(exam => {
    let nRow = myTable.insertRow(-1) , rCell  = 0;

    nRow.insertCell(rCell++).textContent = exam.querySelector('date').textContent;
    nRow.insertCell(rCell++).textContent = exam.querySelector('type').textContent;
    nRow.insertCell(rCell++).textContent = exam.querySelector('course').textContent;
    nRow.insertCell(rCell++).textContent = exam.querySelector('semester').textContent;
    nRow.insertCell(rCell++).textContent = exam.querySelector('students').textContent;
    nRow.insertCell(rCell++).textContent = exam.querySelector('form').textContent;
    nRow.insertCell(rCell++).textContent = exam.querySelector('room').textContent;
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从am XML文件中提取数据并在html表中显示它 - how to pull data from am XML file and show it on html table 如何将数据从 javascript 导入 html 表 - How to import data from javascript to html table 如何在 javascript 中添加按钮,因为在 html 表中导入的数据来自 JSON 文件 - how to add button in javascript since the data that import in the html table is from JSON file 如何使用纯JavaScript将XML文件中的数据解析为HTML表? - How can I parse data from XML file into HTML table using pure JavaScript? 如何从存储在我 PC 上的文件中获取 XML 数据并使用 javascript 以 HTML 格式填充表格? - How can I fetch XML Data from a file stored on my PC and populate a table in HTML using javascript? 从XML提取数据并将其放置在HTML表中 - Extracting data from an XML and placing it in a HTML table 如何读取 XML 文件并使用表中的数据进行验证 - How to read an XML file and validate with the data from a table 如何将XML数据包装在HTML表中? - how to wrap the XML data in table of a HTML? 如何使用 XML 数据动态填充 HTML 表? - How to dynamically populate an HTML Table with XML Data? 如何在asp .net core中导入csv文件并在html表上显示数据然后保存在sql数据库中 - How to import csv file and display the data on html table then save on sql database in asp .net core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM