为什么chrome下不能解析,有其他办法吗?

cjh_tostring 2012-08-23 11:21:06
下面这段代码是按照W3C上写的,不过好像chrome不支持,有其他办法吗?
 <script type="text/javascript">
var xmlDoc=null;
try{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}catch(e){
try {
xmlDoc = document.implementation.createDocument("", "", null);
}catch(ee){ee.message}
}
xmlDoc.async=false;
xmlDoc.load("XML_001.xml");
var username=xmlDoc.getElementsByTagName("username")[0].childNodes[0].nodeValue;
document.write(username);
</script>
...全文
243 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
改一下昵称 2012-08-24
  • 打赏
  • 举报
回复
function loadXML(url)
{
var xmlhttp;
if( window.XMLHttpRequest )
xmlhttp = new XMLHttpRequest();
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open('GET', url);
xmlhttp.onreadystatechange = function(){
if( xmlhttp.readyState==4 && xmlhttp.status==200 ){
return xmlhttp.responseXML;
}
}
xmlhttp.send();
}
AMinfo 2012-08-24
  • 打赏
  • 举报
回复
xmlDoc.load("XML_001.xml");

这个load里面的必须是一个网址,http开头的网址
cjh_tostring 2012-08-24
  • 打赏
  • 举报
回复
XMLHttpRequest好像是解析不了本地的xml文件的是吧?
cjh_tostring 2012-08-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

xmlDoc.load("XML_001.xml");

这个load里面的必须是一个网址,http开头的网址
[/Quote]
这里是可以的,不过就是在chrome下不行。
AMinfo 2012-08-23
  • 打赏
  • 举报
回复

<script type="text/javascript">
var xmlDoc = null;
var xml = "XML_001.xml";
if (window.ActiveXObject) {
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xmlDoc.load(xml);
} else if (document.implementation && document.implementation.createDocument) {
try{
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.async = false;
xmlDoc.load(xml);
} catch(e){
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET",xml,false);
xmlhttp.send(null);
xmlDoc = xmlhttp.responseXML.documentElement;
}
}
var username=xmlDoc.getElementsByTagName("username")[0].childNodes[0].nodeValue;
</script>

87,992

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧