博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Loading XML with Javascript
阅读量:5776 次
发布时间:2019-06-18

本文共 4372 字,大约阅读时间需要 14 分钟。

hot3.png

Loading XML with Javascript

There are three methods I know of that can be used by Javascript to load an XML document for parsing and display in the HTML by Javascript. They are given below, along with a table that shows the results each of these methods will have when loading an XML document by Javascript using the various browsers available for Windows platforms. By a local HTML or local XML file, I mean one which resides on the user's own computer, and by a remote HTML or remote XML file I mean one which resides on the Web and is accessed using the http:// prefix. In cases where both files are remote, they must both be in the same domain (e.g., www.somedomain.com) for the loading of the XML document to succeed. Otherwise, it will fail with an "access is denied" error message. In cases where both files are local, I've tested only cases in which both files are in the same directory and the XML file is accessed simply using its filename (e.g., "test.xml").

ActiveX:
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
while(xmlDoc.readyState != 4) {};
xmlDoc.load(xmlfile);
readXML();
createDocument:
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = readXML;
xmlDoc.load(xmlfile);
XMLHttpRequest:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", xmlfile, false);
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.send("");
xmlDoc = xmlhttp.responseXML;
readXML();
Loaded by Browser⇓         Using⇒ ActiveX createDocument XMLHttpRequest
Local HTML/Local XML IE
Works
Fails
Fails
Chrome
Fails
Fails
Works
Safari
Fails
Fails
Works
Firefox
Fails
Works
Works
Opera
Fails
Works
Works
Local HTML/Remote XML IE
Works
Fails
Works
Chrome
Fails
Fails
Fails
Safari
Fails
Fails
Works
Firefox
Fails
Fails
Fails
Opera
Fails
Fails
Fails
Remote HTML/Remote XML IE
Works
Fails
Works
Chrome
Fails
Fails
Works
Safari
Fails
Fails
Works
Firefox
Fails
Works
Works
Opera
Fails
Works
Works

Using this information, I've come up with the following Javascript code that will load an XML file in just about any situation, if there's a way for that to happen using a given browser. It must be noted that some situations just won't work. For example, if you're doing development using Chrome, Firefox, or Opera, and you have a local HTML file that uses Javascript to load an XML file that resides someplace on the Web, you are trying to do something that just won't work, apparently because of the security precautions built into those browsers. To get the operation of loading a remote XML file with Javascript in a local HTML file to work you'll have to use IE (which will succeed using either the "ActiveX" or "XMLHttpRequest" methods above) or Safari (which will succeed using the "XMLHttpRequest" method above).

This code assumes that readXML is the name of a Javascript function that uses the global variable xmlDoc to access the DOM tree, and that initLibrary() is called with the onload handler from the HTML body tag like this: 

<body οnlοad="initLibrary()">
The file name passed into the importXML function can of course be changed to any local or remote XML file name, depending on which one you want to load.

var xmlDoc;    var xmlloaded = false;    function initLibrary()    {        importXML("http:///www.somedomain.com/somesubdir/somefile.xml");    }    function importXML(xmlfile)    {        try        {            var xmlhttp = new XMLHttpRequest();            xmlhttp.open("GET", xmlfile, false);        }        catch (Exception)        {            var ie = (typeof window.ActiveXObject != 'undefined');            if (ie)            {                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");                xmlDoc.async = false;                while(xmlDoc.readyState != 4) {};                xmlDoc.load(xmlfile);                readXML();                xmlloaded = true;            }            else            {                xmlDoc = document.implementation.createDocument("", "", null);                xmlDoc.onload = readXML;                xmlDoc.load(xmlfile);                xmlloaded = true;            }        }        if (!xmlloaded)        {            xmlhttp.setRequestHeader('Content-Type', 'text/xml')            xmlhttp.send("");            xmlDoc = xmlhttp.responseXML;            readXML();            xmlloaded = true;        }    }

转载于:https://my.oschina.net/tenking/blog/31893

你可能感兴趣的文章
XMLHttpRequest对象如何兼容各浏览器使用?
查看>>
perl智能匹配操作符~~
查看>>
ASCII码对照表
查看>>
每天一个linux命令(22):find 命令的参数详解
查看>>
MySQL修改root密码的各种方法整理
查看>>
虚拟机类加载机制
查看>>
【Quick-Cocos2d-x】 捋一捋框架流程
查看>>
Long基本类型的三个静态方法(java)
查看>>
Siege的源码二次开发&操作手册
查看>>
ECstore meta扩展
查看>>
Oracle 学习之RAC(四) 安装Oracle软件
查看>>
Go Interface
查看>>
apache 403 Forbidden
查看>>
Java 获取当前时间及将时间戳转换成时间显示
查看>>
使用python开发RabbitMQ应用
查看>>
keepalived双主模型实现高可用ipvs的简单案例
查看>>
test01
查看>>
2.linux 日志服务器rsyslog+loganalyzer搭建
查看>>
lvs-NAT-DR-端口绑定
查看>>
在exchange2010中启用outlook anywhere
查看>>