js的xml文件是什么

2025年05月07日 00:05
有1个网友回答
网友(1):

刚学习javascript,写一个小游戏做练习,现在想要做一个配置文件,练习一下XML的操作……
进入正题:
如下xml文件:profile.xml

XML/HTML 



    Hello
    word!

在google搜索结果中出现频率比较高的一段代码:

javascript文件:test.js

JavaScript 

var doc = loadXmlFile("profile.xml");
alert(doc.text);
function loadXmlFile(xmlFile){
  var xmlDom = null;
  if (window.ActiveXObject){
    xmlDom = new ActiveXObject("Microsoft.XMLDOM");
    //xmlDom.loadXML(xmlFile);//如果用的是XML字符串
    xmlDom.load(xmlFile);//如果用的是xml文件。
  }else if (document.implementation && document.implementation.createDocument){
    var xmlhttp = new window.XMLHttpRequest();
    xmlhttp.open("GET", xmlFile, false);
    xmlhttp.send(null);
    xmlDom = xmlhttp.responseXML;
  }else{
    xmlDom = null;
  }
  return xmlDom;
}

这个方法在IE下能正常输出“hello word”,IE9、以及IE9的IE7、IE8的兼容模式都正常。
但是在firefox下输出的是“undefined”
而Chrome下则无输出,提示 xmlhttp.send(null);这行 Uncaught Error: NETWORK_ERR:XMLHttpRequest Exception 101
还有一种方法是用JQuery

JavaScript code?
$.get('profile.xml',function(xml){    
        alert($(xml).text());    
    });

在Chrome下只弹出一个空警告框……