2017-02-21 10 views
0

私の問題は、LoadXMLのサブファンクションコールの間のどこかにあるようです。それは、XMLデータがいくつかの奇妙な理由でnullになるようですが、私はそれを修正する方法は考えていません。 Stackexchangeには同様の質問が多かったようですが、多くの人が回答を得ていなかったり、答えが私のケースを助けなかったりしました。nullのプロパティ 'getElementsByTagName'を読み取ることができません、何が起こっていますか?

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]; 
     } 
    } 
} 

は、ここではいくつかのtypeoだけでなく、いくつかの解析エラーを持っている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

を使用する(私はあなたが取得されていないことを驚いていますWebコンソールのエラーですが、私もそうではありません):XMLの形式が正しくありません: ' '終了タグは' 'ではなく 'wil'です。それを修正し、それを解析し、 'responseXML'が埋め込まれます。 –

+2

暗闇の中で' console.log'トーチで遭遇するのではなく、あなたのブラウザに組み込まれた完全機能のデバッガを使ってライトをオンにすることをお勧めします。それを使って、あなたの 'onreadystatechange'ハンドラで' this'を見て、 'responseXML'プロパティが' null'であることを知ることができます。 –

+0

T.J. CrowderはXMLファイルの型であると書きました。さらに、等価チェックで問題が発生します。 'xmlDoc.getElementsByTagName(" name ")'はコレクションなので、 '.toUpperCase()'はありません。 'xmlDoc.getElementsByTagName(" name ")[i] .innerHTML.toUpperCase()'やそれより短い 'x [i] .innerHTML.toUpperCase()'のような何かをする必要があります。 –

答えて

0

です。 gEBTNは、オブジェクトの配列を返すため

  • getElementsByTagName().toUpperCaseが無効である:こと
    注意。したがって、getElementsByTagName()[i].innerHTML.toUpperCase()を使用する必要があります。
  • 代わりのconsole.log("muuttujan x eka: " + x[0]);を使用して、それはちょうど誤植だconsole.log("muuttujan x eka: " + x[0].innerHTML);

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]; 
 
    } 
 
    } 
 
}
<html> 
 
    <body> 
 
    <input id="name" onblur="load();" /> 
 
    <div id="ComFocus"></div> 
 
    </body> 
 
</html>

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> 
関連する問題