[英]Load xml from javascript
XML完全陌生,我已經為這個非常簡單的目標而苦苦掙扎了很長時間(盡管我可以在Internet上找到足夠多的東西)。 只需要從這個xml文件中獲取值:
<?xml version="1.0" encoding="UTF-8"?>
<materials>
<basic>
<uurloon>10</uurloon>
<setloon>100</setloon>
</basic>
<extra>
<geluid>150</geluid>
<ledset>35</ledset>
<strobo>20</strobo>
<laser>50</laser>
</extra>
</materials>
在javascript中,我使用以下代碼獲取xml數據:
// load xml file
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else { // IE 5/6
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", "pricing.xml", false);
xhttp.send();
xmlDoc = xhttp.responseXML;
var uurloon = xmlDoc.getElementsByTagName("uurloon")[0].childNodes[0].text;
var setloon = xmlDoc.getElementsByTagName("setloon")[0].childNodes[0].text
alert('end');
沒有結果,因為我沒有看到警報..
您的服務器未返回相應的Content-Type
標頭。 responseXML
屬性僅在服務器返回Content-Type: text/xml
或類似的+xml
標頭時才有效。
該服務只需要輸出一個XML Content-type標頭即可。
來自w3c :
如果最終MIME類型不為null,則text / xml,application / xml,並且不以+ xml [...]結尾,返回null。
如果您無權訪問服務器並且無法更改Content-Type
標頭,請使用overrideMimeType
函數強制XMLHttpRequest
將響應視為text/xml
:
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else { // IE 5/6
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.overrideMimeType('text/xml');
xhttp.open("GET", "pricing.xml", false);
xhttp.send(null);
xmlDoc = xhttp.responseXML;
var uurloon = xmlDoc.getElementsByTagName("uurloon")[0].childNodes[0].text;
var setloon = xmlDoc.getElementsByTagName("setloon")[0].childNodes[0].text
alert('end');
引用: http : //blog-rat.blogspot.com/2010/11/xmlhttprequestresponsexml-returns-null.html
// load xml file
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else { // IE 5/6
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", "pricing.xml", false);
xhttp.send(null);
xhttp.onreadystatechange = function(){
if (xhttp.status == "200")
xmlDoc = xhttp.responseXML;
}
var uurloon = xmlDoc.getElementsByTagName("uurloon")[0].childNodes[0].textContent;
var setloon = xmlDoc.getElementsByTagName("setloon")[0].childNodes[0].textContent;
alert('end');
我使用一個名為price.xml的文件進行了簡單測試:
<?xml version="1.0" encoding="UTF-8"?>
<materials>
<basic>
<uurloon>10</uurloon>
<setloon>100</setloon>
</basic>
<extra>
<geluid>150</geluid>
<ledset>35</ledset>
<strobo>20</strobo>
<laser>50</laser>
</extra>
</materials>
和HTML與這段代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body onload="init()">
<p>Hola</p>
<script>
function init(){
// load xml file
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else { // IE 5/6
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", "price.xml", false);
xhttp.send();
xmlDoc = xhttp.responseXML;
var uurloon = xmlDoc.getElementsByTagName("uurloon")[0].childNodes[0].textContent;
var setloon = xmlDoc.getElementsByTagName("setloon")[0].childNodes[0].textContent;
console.log(uurloon,setloon); //give me "10 100"
}
</script>
</body>
</html>
並為我工作。 我認為你失敗了因為你正在調用.text
屬性而不是.textContent
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.