//兼容方法
        function getXMLDOM(xmlStr) {
            var xmlDom = null;
            if (typeof window.DOMParser != "undefined") {
                xmlDom = (new DOMParser).parseFromString(xmlStr, "text/xml");
                var errors = xmlDom.getElementsByTagName("parsererror");
                if (errors.length > 0) {
                    throw new Error("DOM2模型错误信息" + errors[0].textContent);
                }


                                                    }


            else if (typeof window.ActiveXObject != "undefined") {
                var version = ['MSXML2.DOMDocument.6.0', 'MSXML2.DOMDocument.3.0', 'MSXML2.DOMDocument'];
                for (var i = 0; i < version.length; i++)  {
                    try {
                        var xmlDom = new ActiveXObject(version[i]);

                         }

                    catch (e) {
                        //跳过
                                  }
                                                                       }
                //放在循环外面才能报错
                xmlDom.loadXML(xmlStr); //载入字符串
                //xml序列报错
                if (xmlDom.parseError != 0) {
                    throw new Error("错误信息" + xmlDom.parseError.reason);
                    return xmlDom;
                                                         }

            }

            else { throw new Error('您的系统或浏览器不支持XML DOM对象!'); }

            return xmlDom;

            }

 

 

 

        //序列化
        function serializerXML(xmlDom) {
            var xml = "";
            if (typeof window.XMLSerializer != "undefined") {
                xml = (new XMLSerializer()).serializeToString(xmlDom);
            }
            else if (typeof xmlDom.xml != "undefined") {
                xml = xmlDom.xml;

            }
            return xml;
        }

 

 

       var xmlStr = "<root>\n<user>CPU123</user>\n</root>";xml加载字符串


        var xmlDom = getXMLDOM(xmlStr);//调用兼容方法得到一个xml DOM对象


        alert(serializerXML(xmlDom));//调用XML序列化
    
    为了跨越所有浏览器兼容,外面放弃了从外部加载XML文件,而使用了字符
    串XML加载和序列化

posted on 2013-11-17 10:40  cpu歌歌  阅读(210)  评论(0编辑  收藏  举报