如何在JavaScript中加载XML文件,从中获取特定信息,然后通过alert()显示它?

时间:2018-04-13 19:47:45

标签: javascript html xml

所以,我想创建一个能够获取用户数据的程序(用逗号分隔大量的bug名称),通过,获取每个数据,然后在表格中显示所有数据。我经历了5次这样的重写,但仍然没有任何反应。有人知道这段代码有什么问题吗?

我的HTML代码:

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
  <h1 align="center">Bugs</h1>
  <p>In the area below, type in each of the insects you want information 
about, seperate them with a comma.</p>
  <textarea id="insects"></textarea>
  <br>
  <button type="button" id="go">Find out!</button>
  <br>
  <script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
document.getElementById('go').onclick = function() {
var input = document.getElementById('insects').value;
var splitted = input.split(',');
for (i = 0; i<splitted.length; i++) {
    var bug1 = splitted[i];
    var part1 = // I need to assign it to bug1 without any whitespace
    var finish = part1.toLowerCase();
    find1(bug1);
  }
}
function find1(bug) {
  var xmlDocument = $.parseXML("externalfile:drive-77136639a78ffb21e72c7c4dfe7f7bb73604aeb3/root/Bugs/bugs.xml");
  var pain = $(xmlDocument).find("value[type='" + bug + "'] pain").text();
  alert(pain); <!-- This is to see if it works -->
}
  </script>
</body>

这是我的XML代码(顺便说一句,XML文件名为bugs.xml,是的,它们位于My Drive/Bugs中的同一个文件夹中:

<?xml version="1.0" encoding="UTF-8"?>
<bugs>
  <bug type="blisterbeetle">
    <name>Blister Beetle</name>
    <pain>55</pain>
    <conservation></conservation>
    <habitat></habitat>
    <rarity></rarity>
    <class></class>
    <order></order>
    <family></family>
    <species></species>
    <dangerous></dangerous>
    <external></external>
  </bug>
</bugs>

0 个答案:

没有答案