Loading XML with Javascript
来源:互联网 发布:怎样与淘宝买家联系 编辑:程序博客网 时间:2024/05/24 07:43
转载:http://www.steveborn.com/codenotes/LoadingXML.htm
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();
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 variablexmlDoc to access the DOM tree, and thatinitLibrary() is called with the onload handler from the HTMLbody tag like this:
<body onload="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; } }
- Loading XML with Javascript
- Loading images with GDI+
- Tutorial: Loading and parsing external XML and JSON files with Unity
- [ERROR] [1513239243.491012935]: Skipped loading plugin with error: XML Document '/opt/ros/kinetic/sh
- javascript+css ---Loading效果
- javascript loading效果
- javascript C# loading
- Lazy Loading Asyncronous Javascript
- Loading JavaScript without blocking
- javascript的lazy loading
- how javascript loading works
- Image loading with Imaging API
- Synchronize with Driver loading/Unloading
- Asynchronous JavaScript and XML (AJAX) with Java 2 Enterprise Edition
- Read and Display Server-Side XML with JavaScript
- AJAX: Creating Web Pages with Asynchronous JavaScript and XML
- How to Load XML with JavaScript on an HTML5 Page
- Loading Menus Through XML Files
- cookie 工作原理
- 【C++ Primer】标准库string类型
- 机器学习算法中文视频教程
- LOWORD与HIWORD,GetEditSel与SetEditSel
- typedef四用途与两陷阱
- Loading XML with Javascript
- svn 常见图标意义
- MySQL随机查询的优化实例
- MYSQL数据丢失讨论
- Android: ListView与Button的共存问题解决
- HashMap
- php 中 define 定义常量和单双引号问题
- Android杀毒实现原理及实例
- TABLE CONTROL TEST-1