JS类库——XMLHelper

XMLHelper.js

ContractedBlock.gif ExpandedBlockStart.gif Code
//取得XML对象
var XMLHelper = function() {}

XMLHelper.prototype 
= {
    
//将xml文件生成dom对象
    loadXMLDoc:function(filePath) {
        
var xmlDoc = null;
        
try {
            
//Internet Explorer
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        }
        
catch(e) {
            
try {
                
//Firefox, Mozilla, Opera
                xmlDoc = document.implementation.createDocument(""""null);
            }
            
catch(ex) {
                
//alert(ex.message);
            }
        }
        
try {
            xmlDoc.async 
= false;
            xmlDoc.load(filePath);
            
//document.write("xmlDoc is loaded, ready for use");
        }
        
catch(e) {
            
//alert(e.message);
        }
        
return xmlDoc;
    },
    
//将xml字符串生成dom对象
    loadXML:function(xmlstring) {
        
var xmlDoc = null;
        
try {
            
//Internet Explorer
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async 
= "false";
            xmlDoc.loadXML(xmlstring);
        }
        
catch(e) {
            
try {
                
//Firefox, Mozilla, Opera
                var parser = new DOMParser();
                xmlDoc 
= parser.parseFromString(xmlstring, "text/xml");
            }
            
catch(ex) {
                
//alert(ex.message)
            }
        }
        
return xmlDoc;
    },
    
//将xml文件按xsl输出
    formatToString:function(xmlDoc, xslDoc) {
        
var str = "";
        
if(document.all) {
            
//Internet Explorer
            str = xmlDoc.documentElement.transformNode(xslDoc);
        }
        
else {
            
//Firefox, Mozilla, Opera
            var oXsltProcessor = new XSLTProcessor();
            oXsltProcessor.importStylesheet(xslDoc);
            
var oNewDOM = oXsltProcessor.transformToDocument(xmlDoc);
            
var oSerializer = new XMLSerializer();
            str 
= oSerializer.serializeToString(oNewDOM);
        }
        
return str;
    }
}

var __XML__ = new XMLHelper();

 

下载

转载于:https://www.cnblogs.com/skey_chen/archive/2009/11/25/1610216.html

你可能感兴趣的:(JS类库——XMLHelper)