私はhtmlとjavascriptを初めて使用しています。私は解析し、javascriptコードを介してXMLファイルのデータにアクセスしようとしています。現在はnullを示しています。私は下に私のコードを掲載しています。一見して助けてください。Xml DOM解析中null
Html code:
<!DOCTYPE html>
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<body>
<p id="demo"></p>
<script>
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "http://10.21.64.222/LoadBreakSwitch/LBS_Commands.xml", true);
xhttp.send();
function myFunction(xml) {
var x, i, txt, xmlDoc;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName("Ping");
for (i = 0; i < x.length; i++) {
txt += x[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>
The xml file:
<?xml version="1.0" encoding="utf-8"?>
<LBSCommands>
<Ping Commkey="A3070000AA00A4" Value="A309008001043101A4"/>
<Frequency Commkey="A3070300AD00A4" CommValue="A309038001013101A4"/>
<SwitchStatus Commkey="A3071D01C800A4" CommValue="A3091D8101014C01A4"/>
</LBSCommands>
コードは正常です。 nullの '' Ping ''のnodeValueにアクセスしています。おそらく、 'nodeValue'の代わりに' getAttribute( "Commkey") 'や' getAttribute( "Value") 'を実行したいと思うでしょう。 nodeValueを取得したい場合、Pingは '私のping 'のようなものでなければならず、nodeValueは "my ping" 'になります。 –
phoa
@phoa-返信いただきありがとうございます。しかし、getAttributeを使用しても、エラーが発生しています。 –
私は 'txt + = x [i] .getAttribute( 'Value')+"
"を意味しました。 – phoa