无法读取null的属性'getElementsByTagName',发生了什么?

时间:2017-02-21 09:07:36

标签: javascript xml xmlhttprequest

我的问题似乎介于LoadXML的子函数调用之间。由于一些奇怪的原因,似乎xml数据变为null,我不知道如何解决这个问题。 Stackexchange似乎有很多类似的问题,但很多都没有得到答复,或者他们的答案对我的案例没有帮助。

function load() {
    var xmlhttp;
    xmlhttp = new XMLHttpRequest();
    var idname = document.getElementById("name").value;
    xmlhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            console.log(this); //it obtains it here...
            LoadXML(this, idname);
        }
    };
    xmlhttp.open("GET", "helper_database.xml", false);
    xmlhttp.overrideMimeType('text/xml');
    xmlhttp.send();
}

function LoadXML(xml, name) {
    var x, i, xmlDoc, nametxt, areEqual;
    xmlDoc = xml.responseXML;
    nametxt = name;
    console.log("HERE \n" + xmlDoc); //...but it becomes null.
    x = xmlDoc.getElementsByTagName("name"); //this returns the error of "Cannot read property 'getElementsByTagName' of null"
    console.log("muuttujan x eka: " + x[0]);
    for (i = 0; i < x.length; i++) {
        if (areEqual = xmlDoc.getElementsByTagName("name").toUpperCase() === nametxt.toUpperCase()) {
            document.getElementById("ComFocus").value = x[i];
        }
    }
}

这是helper_database.xml

<Character>
    <name>test</name>
    <stats>
        <Com>1</Com>
        <Con>2</Con>
        <Cun>3</Cun>
        <Dex>4</Dex>
        <Mag>5</Mag>
        <Per>6</Per>
        <Str>7</Str>
        <Wil>8</wil>
    </stats>
</Character>

1 个答案:

答案 0 :(得分:0)

你有一些类型和一些解析错误 请注意:

  • getElementsByTagName().toUpperCase无效,因为gEBTN返回对象数组。所以,你必须使用getElementsByTagName()[i].innerHTML.toUpperCase()
  • 使用console.log("muuttujan x eka: " + x[0]);
  • 而不是console.log("muuttujan x eka: " + x[0].innerHTML);

&#13;
&#13;
function load() {
  var xmlhttp;
  xmlhttp = new XMLHttpRequest();
  var idname = document.getElementById("name").value;
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      console.log(xmlhttp); //it obtains it here...
      LoadXML(xmlhttp, idname);
    }
  };
  xmlhttp.open("GET", "helper_database.xml", false);
  //xmlhttp.overrideMimeType('text/xml');
  xmlhttp.send();
}

function LoadXML(xml, name) {
  var x, i, xmlDoc, nametxt, areEqual;
  xmlDoc = xml.responseXML;
  nametxt = name;
  console.log("HERE \n" + xmlDoc); //...but it becomes null.
  x = xmlDoc.getElementsByTagName("name"); //this returns the error of "Cannot read property 'getElementsByTagName' of null"
  console.log("muuttujan x eka: " + x[0].innerHTML);
  for (i = 0; i < x.length; i++) {
    if (areEqual = xmlDoc.getElementsByTagName("name")[0].innerHTML.toUpperCase() === nametxt.toUpperCase()) {
      document.getElementById("ComFocus").value = x[i];
    }
  }
}
&#13;
<html>
  <body>
    <input id="name" onblur="load();" />
    <div id="ComFocus"></div>
  </body>
</html>
&#13;
&#13;
&#13;

<强> XML

<?xml version="1.0" encoding="UTF-8"?>
<Character><name>test</name>
<stats>
<Com>1</Com>
<Con>2</Con>
<Cun>3</Cun>
<Dex>4</Dex>
<Mag>5</Mag>
<Per>6</Per>
<Str>7</Str>
<Wil>8</Wil>
</stats></Character>