几个javascript处理xml的函数 - 1

// Based on <http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1950641247> (copy from ajaxslt)

var DOM_ELEMENT_NODE = 1;
var DOM_ATTRIBUTE_NODE = 2;
var DOM_TEXT_NODE = 3;
var DOM_CDATA_SECTION_NODE = 4;
var DOM_ENTITY_REFERENCE_NODE = 5;
var DOM_ENTITY_NODE = 6;
var DOM_PROCESSING_INSTRUCTION_NODE = 7;
var DOM_COMMENT_NODE = 8;
var DOM_DOCUMENT_NODE = 9;
var DOM_DOCUMENT_TYPE_NODE = 10;
var DOM_DOCUMENT_FRAGMENT_NODE = 11;
var DOM_NOTATION_NODE = 12;

/**
* http://developer.mozilla.org/en/docs/DOMParser
* 接受一段xml,解析成为xml document对象
* 调用时可用try{}catch(e){}捕捉错误,错误对象具有parser xml message属性
* @param {String} xml string
* @return {Object} xml document object
*/
xmlDocument(/* String */xmlString){
    var doc = null;

    if (window.ActiveXObject){
        var ActiveIds = ['MSXML2.XMLDOM', 'Microsoft.XMLDOM', 'MSXML.XMLDOM', 'MSXML3.XMLDOM'];
        for (var len = ActiveIds.length, i = 0; i < len; i++){
            var id = ActiveIds[i];
            try{
                var doc = new ActiveXObject(id);
                doc.async = false;
                doc.setProperty('SelectionLanguage', 'XPath');
                doc.loadXML(xmlString);
                break;
            }catch (e){}finally{
                if (doc && doc.parseError && doc.parseError.errorCode != 0){
                    throw {parser:'MSXML', message:doc.parseError.reason, xml:xmlString, func:'xmlDocument'};
                }
            }
        }
    }else if (typeof DOMParser != 'undefined'){
        var parser = new DOMParser();
        var doc = parser.parseFromString(xmlString, 'text/xml');
        if (doc.documentElement.nodeName == 'parsererror'){
            throw {parser:'DOMParser', message:doc.documentElement.firstChild.nodeValue, xml:xmlString, func:'xmlDocument'};
        }
    }else{ return false; }

    return doc;
}

/**
* 获取指定节点或文档对象的xml内容
* port from ajaxslt xmlText()
* @param {Object} xml DOM Node or xml Document
* @return {String} xml string
*/
xmlText(/* Document|Element */node){
    if (typeof node == 'string'){ return node; }

    if (node.innerXML){
        return node.innerXML;
    }else if (node.xml){
        return node.xml;
    }else if (typeof XMLSerializer != 'undefined'){
        return (new XMLSerializer()).serializeToString(node);
    }else{ return false; }
}

/**
* 获取所有节点的内容
* port from ajaxslt xmlValue()
* @param {Object} xml DOM Node or xml Document
* @return {String}
*/
xmlValue(/* Document|Element */node){
    var val = '';
    if (node.nodeType == DOM_TEXT_NODE ||
        node.nodeType == DOM_CDATA_SECTION_NODE ||
        node.nodeType == DOM_ATTRIBUTE_NODE){
        val += node.nodeValue;
    }else if (node.nodeType == DOM_ELEMENT_NODE ||
              node.nodeType == DOM_DOCUMENT_NODE ||
              node.nodeType == DOM_DOCUMENT_FRAGMENT_NODE){
        for (var len = node.childNodes.length, i = 0; i < len; i++){
            val += arguments.callee(node.childNodes[i]);
        }
    }
    return val;
}

你可能感兴趣的:(JavaScript,xml,Microsoft)