摘要:     JScript读取XML文件需要考虑msxml组件的多个版本,稳妥的做法是将所有版本都列出来。

小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

     JScript读取XML文件需要考虑msxml组件的多个版本,稳妥的做法是将所有版本都列出来。

xml文件的内容如下:

[html]
<?xml version="1.0" encoding="GB2312" ?>  
<Binary> 
     <Item name="a.exe" /> 
     <Item name="b.dll" /> 
     <Item name="c.dll" /> 
</Binary>  

<?xml version="1.0" encoding="GB2312" ?>
<Binary>
     <Item name="a.exe" />
     <Item name="b.dll" />
     <Item name="c.dll" />
</Binary> 

 

读取该xml文件的JScript如下:

[javascript]
main(); 
 
function main()  

var xmlDoc = null; 
       var signatures = [ 
                   "Msxml2.DOMDocument.6.0", 
                   "Msxml2.DOMDocument.5.0", 
                   "Msxml2.DOMDocument.4.0", 
                   "Msxml2.DOMDocument.3.0", 
                   "Msxml2.DOMDocument", 
                   "Microsoft.XmlDom"]; 
        for (var index = 0; index < signatures.length; index++)  
      { 
            try { 
                xmlDoc = new ActiveXObject(signatures[index]); 
                 } 
            catch (e)  
        { 
                //继续尝试更低版本  
                        } 
        }    
xmlDoc.async = false; 
xmlDoc.resolveExternals = false; 
var strXmlPath = “D://test.xml”; 
xmlDoc.load(strXmlPath); 
if (xmlDoc.parseError.errorCode != 0) 
      { 
            var myError = xmlDoc.parseError; 
            WScript.Echo("打开xml文件失败: " + myError); 
        } 
        else  
       { 
            var BinGroup = xmlDoc.getElementsByTagName("Item"); 
                             for (var i = 0; i < BinGroup.length; i++) 
                { 
                                          var strBinName = BinGroup.item(i).getAttribute("name"); 
                                  WScript.Echo(strBinName); 
            }  
         } 

main();

function main()
{
var xmlDoc = null;
       var signatures = [
                   "Msxml2.DOMDocument.6.0",
                   "Msxml2.DOMDocument.5.0",
                   "Msxml2.DOMDocument.4.0",
                   "Msxml2.DOMDocument.3.0",
                   "Msxml2.DOMDocument",
                   "Microsoft.XmlDom"];
        for (var index = 0; index < signatures.length; index++)
      {
            try {
                xmlDoc = new ActiveXObject(signatures[index]);
                 }
            catch (e)
  {
                //继续尝试更低版本
                        }
        } 
xmlDoc.async = false;
xmlDoc.resolveExternals = false;
var strXmlPath = “D://test.xml”;
xmlDoc.load(strXmlPath);
if (xmlDoc.parseError.errorCode != 0)
      {
            var myError = xmlDoc.parseError;
            WScript.Echo("打开xml文件失败: " + myError);
        }
        else
       {
      var BinGroup = xmlDoc.getElementsByTagName("Item");
                             for (var i = 0; i < BinGroup.length; i++)
        {
                                          var strBinName = BinGroup.item(i).getAttribute("name");
                               WScript.Echo(strBinName);
   }
         }
}

 

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!

本文固定链接: http://www.js-code.com/js/js_8815.html