如何使用javascript在google chrome中加载xml文件
我是网络编程的初学者,也是这个网站的新手。我试图获取xml标记的内容,但是当我在chrome中打开下面的代码时,什么都没有出现。有人能给我一个提示吗? 以下是完整的代码:如何使用javascript在google chrome中加载xml文件,javascript,xml,Javascript,Xml,我是网络编程的初学者,也是这个网站的新手。我试图获取xml标记的内容,但是当我在chrome中打开下面的代码时,什么都没有出现。有人能给我一个提示吗? 以下是完整的代码: <!DOCTYPE html> <html> <head> <script> var xmlDoc, xmlHttp; function readxml() { xmlHttp = new window.XMLHttpRequest(); xmlHttp.ope
<!DOCTYPE html>
<html>
<head>
<script>
var xmlDoc, xmlHttp;
function readxml()
{
xmlHttp = new window.XMLHttpRequest();
xmlHttp.open("GET","books.xml",false);
xmlHttp.send(null);
xmlDoc = xmlHttp.responseXML.documentElement;
return xmlDoc;
}
</script>
</head>
<body>
<script>
var xmld
xmld=readxml();
document.write(xmld.getElementsByTagName("title")[0].childNodes[0].nodeValue + "<br>");
document.write(xmld.getElementsByTagName("author")[0].childNodes[0].nodeValue + "<br>");
document.write(xmlDoc.getElementsByTagName("year")[0].childNodes[0].nodeValue);
</script>
</body>
</html>
var-xmlDoc,xmlHttp;
函数readxml()
{
xmlHttp=newwindow.XMLHttpRequest();
open(“GET”,“books.xml”,false);
xmlHttp.send(空);
xmlDoc=xmlHttp.responseXML.documentElement;
返回xmlDoc;
}
变量xmld
xmld=readxml();
document.write(xmld.getElementsByTagName(“title”)[0]。childNodes[0]。nodeValue+“
”;
document.write(xmld.getElementsByTagName(“作者”)[0]。childNodes[0]。nodeValue+“
”;
document.write(xmlDoc.getElementsByTagName(“年”)[0].childNodes[0].nodeValue);
提示:函数readxml
在加载xml之前返回。因此,您试图在拥有XML内容之前将其写入文档
查看本例中的reqListener
: