当前位置: 代码迷 >> XML/SOAP >> javascript提取xml信息出错,帮忙看一下,该如何解决
  详细解决方案

javascript提取xml信息出错,帮忙看一下,该如何解决

热度:308   发布时间:2012-03-26 15:46:56.0
javascript提取xml信息出错,帮忙看一下
很简单的note.xml和note.htm,用javascript提取xml 中的信息,不知道哪里出错了,help
note.xml:
<?xml version="1.0" encoding="ISO-8859-1" ?> 
<!-- Copyright w3school.com.cn --> 
 <note>
  <to id = "to">George</to> 
  <from id = "from">John</from> 
  <heading>Reminder</heading> 
  <body id = "body">Don't forget the meeting!</body> 
</note>
note.htm:
<html>
<head>
<script type="text/javascript">
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  document.write("IE7+, Firefox, Chrome, Opera, Safari");
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  document.write("IE6,5"); //我自己加的,程序可以运行到这里
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","note.xml",false);
xmlhttp.send();

xmlDoc=xmlhttp.responseXML; //程序运行到这里,就不执行了,所以我怀疑是下面的出错。。。

document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;

document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;

document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
</script>
</head>

<body>
<h1>W3School.com.cn Internal Note</h1>
<p><b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span><br />
<b>Message:</b> <span id="message"></span>
</body>
</html>
我还以为没有调用head里面 script中的内容,但是加上document.write也会显示信息,说明是运行了

------解决方案--------------------
看看document.write(xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue);
有值吗?
  相关解决方案