zoukankan      html  css  js  c++  java
  • JavaScript操作XML(一)

    IE篇
    Miscrosoft基于ActiveX来操作XML的库叫MSXML,而要用XML的操作,用户需要安装MSXML,不过IE5.0或以上都已在其浏览器嵌入了MSXML;到目前为止已有很多个版本了,最初的版本叫Microsoft.XmlDom,建立其对象实例可以这样写:

    var oXmlDom = new ActiveXObject("Miscrosoft.XmlDom");

    目前MSXML版本已民展到5.0了,以下是其可用的版本:
    ❑ Microsoft.XmlDom (original)
    ❑ MSXML2.DOMDocument
    ❑ MSXML2.DOMDocument.3.0
    ❑ MSXML2.DOMDocument.4.0
    ❑ MSXML2.DOMDocument.5.0

    因为新版本的MSXML可能会提升其速度或支持一些特性比如校验,一般来说,我们当然希望用到最新的版本,所以我们可以这样来创建客户端浏览器支持MSXML的最新版本:

    function createXMLDOM()
                {
                    
    var arrSignatures = ["MSXML2.DOMDocument.5.0",
                                         
    "MSXML2.DOMDocument.4.0",
                                         
    "MSXML2.DOMDocument.3.0",
                                         
    "MSXML2.DOMDocument",
                                         
    "Microsoft.XmlDom"];
                    
    for (var i = 0;i < arrSignatures.length; i++)
                    {
                        
    try
                        {
                            
    var oXmlDom = new ActiveXObject(arrSignatures[i]);
                            
    return oXmlDom;
                        }
                        
    catch(oError)
                        {
                            
    //ignore
                        }
                        
    throw new Error("MSXML is not installed on your system.");
                    }                     
                }

    #加裁XML
    我们可以用loadXML()或load()方法来加载XML
    例如:
    var oXmlDom = createXMLDOM();
                    
                    oXmlDom.loadXML(
    "<root><child/></root>");

    oXmlDom.load(
    "test.xml");
    加载XML支持同步和异步加载,MSXML默认是异步加载XML文件的,若要同步加载XML文件可以这样做:
    oXmlDom.async = false;
    在加载XML文件时可以选部分、相对、绝对路径如:
    oXmlDom.load("test.xml");
    oXmlDom.load("../test.xml");
    oXmlDom.load("http://test.xml");
    加载XML文件时有个状态属性叫做readyState,其有五个值:
    ❑ 0 — The DOM hasn’t been initialized with any information.
    ❑ 1 — The DOM is loading data.
    ❑ 2 — The DOM has completed loading the data.
    ❑ 3 — The DOM may be used although some sections may not be available.
    ❑ 4 — The DOM is completely loaded and ready to be used.

    当readyState的值改变值会触发一个onreadystatechange事件:
    oXmlDom.onreadystatechange = function()
                    {
                        
    /**
                         * readyState:
                         * 0-- The Dom hasn't been initialized with any information
                         * 1-- The Dom is loading data;
                         * 2-- The Dom has completed loading the data;
                         * 3-- The Dom may be usedd although some sections mays not be avaible;
                         * 4-- Th Dom is complately loaded and ready to be used;
                         
    */
                        alert(oXmlDom.readyState);
                    }

    当我们加载完XML文件后如何取得XML文件的值呢?
    我们可以用这样的语句:alert(oXmlDom.xml);
    转载请注明出处[http://samlin.cnblogs.com/

    欢迎关注本人公众号:

    作者赞赏
  • 相关阅读:
    java修饰符 protect public protected
    java中interface使用
    java中super的用法
    引用的一道JAVA题目
    java中==和equals的区别(转)
    2019PHP面试题最全面归纳总结
    (一)PHP基础知识考察点
    Linux常用命令大全(非常全!!!)
    MAMP mysql无法启动 总结(以后有发现再添加)
    win 安装composer (详细教程)
  • 原文地址:https://www.cnblogs.com/samlin/p/1006310.html
Copyright © 2011-2022 走看看