Loading XML with Javascript

本文详细介绍了使用JavaScript加载XML文件的三种方法,并通过表格形式展示了不同浏览器对本地和远程文件的加载兼容性。重点讨论了ActiveX、createDocument和XMLHttpRequest方法在各种情况下的应用效果,帮助开发者了解如何在不同环境下实现XML文件的高效加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 转载: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();




Loaded by Browser⇓         Using⇒ActiveXcreateDocumentXMLHttpRequest
Local HTML/Local XMLIE
Works
Fails
Fails
Chrome
Fails
Fails
Works
Safari
Fails
Fails
Works
Firefox
Fails
Works
Works
Opera
Fails
Works
Works
Local HTML/Remote XMLIE
Works
Fails
Works
Chrome
Fails
Fails
Fails
Safari
Fails
Fails
Works
Firefox
Fails
Fails
Fails
Opera
Fails
Fails
Fails
Remote HTML/Remote XMLIE
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 variablexmlDoc to access the DOM tree, and that initLibrary() 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;
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值