码迷,mamicode.com
首页 > 其他好文 > 详细

XML

时间:2014-09-19 13:27:05      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:io   os   ar   for   sp   cti   on   c   ad   

//跨浏览器处理xml
    //创建空白文档
    function createDocument() {
        if ( typeof arguments.callee.activeXString != "string" ) {
            var versions = ["MSXML2.DOMDocument.6.0", "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument"];

            for ( var i = 0, len = versions.length; i < len; i++ ) {
                try {
                    new ActiveXObject( versions[i] );
                    arguments.callee.activeXString = versions[i];
                    break
                }
                catch ( ex ) {

                }
            }
        }
        return new ActiveXObject( arguments.callee.activeXString );
    }
    function parsXml( xml ) {
        var xmlDom = null;

        if ( typeof DOMParser != "undefined" ) {
            var errors = null;

            xmlDom = (new DOMParser()).parseFromString( xml, "text/xml" );
            errors = xmlDom.getElementsByTagName( "parsererror" );
            if ( errors.length ) {
                throw new Error( "XML parsing error: " + errors[0].textContent );
            }
        }
        else if ( typeof ActiveXObject != "undefined" ) {
            xmlDom = createDocument();
            xmlDom.loadXML( xml );
            if ( xmlDom.parseError != 0 ) {
                throw new Error( "XML parsing error: " + xmlDom.parseError.reason );
            }
        }
        else {
            throw new Error( "No XML parser available" );
        }

        return xmlDom;
    }

    function serializeXml(xmlDom){
        if(typeof XMLSerializer!="undefined"){
            return (new XMLSerializer()).serializeToString(xmlDom);
        }else if(typeof xmlDom.xml!="undefined"){
            return xmlDom.xml;
        }else{
            throw new Error( "Can not serialize XML DOM" );
        }
    }


    //xPath 实现XML的DOM查找
    var supportsXPath=document.implementation.hasFeature("XPath","3.0");

XML

标签:io   os   ar   for   sp   cti   on   c   ad   

原文地址:http://www.cnblogs.com/zodiacblog/p/3981101.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!