用js實現xml模糊查找

相關xml文件內容如下:

 

js如下:

// 錯誤信息常量
var MSG_INVALID_XMLFILE = "您的瀏覽器不支持xml文件讀取,於是本頁面禁止您的操作,推薦使用IE5.0以上可以解決此問題!";
 
function loadXML(xmlFilePath)
    {
      var _xmlDoc = null;
      if (!window.DOMParser && window.ActiveXObject)
      {
        var xmlDomVersions = ['MSXML2.DOMDocument.6.0','MSXML2.DOMDocument.3.0','Microsoft.XMLDOM'];
        for (var i = 0; i < xmlDomVersions.length; i++)
        {
          try
          {
            _xmlDoc = new ActiveXObject(xmlDomVersions[i]);
            break;
          }
          catch(e){}
        }
      }
      else if (document.implementation && document.implementation.createDocument)
      {
        try
        {
          _xmlDoc = document.implementation.createDocument('','',null);
        }
        catch(e){}
      }

      if (_xmlDoc != null)
      {
        _xmlDoc.async = false;
        _xmlDoc.load(xmlFilePath);
      }
     

      return _xmlDoc;
    }
// 檢測 生成xml文檔
function checkXMLDocObj(xmlFile)

 var xmlDoc = loadXML(xmlFile);  
 if(xmlDoc == null)
 {
  alert(MSG_INVALID_XMLFILE);  
  return false ;
 }
 return xmlDoc;
}

//使FIREFOX支持selectNodes()、selectSingleNode()

// check for XPath implementation
if( document.implementation.hasFeature("XPath", "3.0") )
{
// prototying the XMLDocument
XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
{
if( !xNode ) { xNode = this; }
var oNSResolver = this.createNSResolver(this.documentElement)
var aItems = this.evaluate(cXPathString, xNode, oNSResolver,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
var aResult = [];
for( var i = 0; i < aItems.snapshotLength; i++)
{
aResult[i] = aItems.snapshotItem(i);
}
return aResult;
}

// prototying the Element

Element.prototype.selectNodes = function(cXPathString)
{
if(this.ownerDocument.selectNodes)
{
   return this.ownerDocument.selectNodes(cXPathString, this);
}
else{throw "For XML Elements Only";}
}
}


// check for XPath implementation
if( document.implementation.hasFeature("XPath", "3.0") )
{
// prototying the XMLDocument
XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
{
if( !xNode ) { xNode = this; }
var xItems = this.selectNodes(cXPathString, xNode);
if( xItems.length > 0 )
{
return xItems[0];
}
else
{
return null;
}
}

// prototying the Element
Element.prototype.selectSingleNode = function(cXPathString)
{
if(this.ownerDocument.selectSingleNode)
{
return this.ownerDocument.selectSingleNode(cXPathString, this);
}
else{throw "For XML Elements Only";}
}
}
      function fnIEParse(path){  
          alert(path);  
          var nodes = xmlDoc.selectNodes(path);  
          for(var i=0;i<nodes.length;i++){  
              var node = nodes[i];  
              alert(node.xml);  
           }  
     }  
  function fnFirefoxParse(path){ 
        alert(path); 
        var oNSResolver = xmlDoc.createNSResolver(xmlDoc.documentElement); 
        var nodes = xmlDoc.uate(xpath,xmlDoc, oNSResolver,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); 
        alert("");  
      }  
  function fnSelectNodes(){  
       var path=document.getElementById_r("path").value;  
       if(exploer=='ie'){  
       fnIEParse(path);  
       }else if(exploer='firefox'){  
        fnFirefoxParse(path);  
       }else{  
        alert("由於你的瀏覽器不支持Javascript,path...");    
            }  
       }  
    function checkbrowser()
    {
         var Sys = {};
       var ua = navigator.userAgent.toLowerCase();
       if (window.ActiveXObject)
             Sys.ie = ua.match(/msie ([/d.]+)/)[1]
        else if (document.getBoxObjectFor)
             Sys.firefox = ua.match(/firefox//([/d.]+)/)[1]
          
      if(Sys.firefox)  //firefox
      {
     return true;
       }
       else
 return false;
   }

//根據關鍵字查找相關文章

function ShowRelatedInfo(keyword1,keyword2,xmlfile,Relatedtitle)
{
    var xmlDoc = checkXMLDocObj(xmlfile) ;
  
    var nodelist=null;
       var nodelist1=null;
        var title1='';
     var title2='';
     var URL='';
        var str = '';
        if(Relatedtitle=="近期熱點")
        {
          nodelist=xmlDoc.getElementsByTagName("info");
           for (i=0;i<nodelist.length;i++)
  {
 
    title1=nodelist[i].getAttribute("title");
       URL=nodelist[i].getAttribute("url"); 
        str+='<li><a href=/"'+URL+'/" target="/_blank/">'+title1+'</a></li>';
}

     }
else
 {
    if(checkbrowser())  //firefox
    {
       nodelist =xmlDoc.selectNodes("//info[contains(@keyword,'"+keyword1+"')]"); 
      if((keyword2!='')&&(nodelist.length<9))//第二個關鍵字不爲空且第一個關鍵字的文章少於9篇,
           nodelist1 =xmlDoc.selectNodes("//info[contains(@keyword,'"+keyword2+"')]");
    }
   else
 {
     xmlDoc.setProperty("SelectionLanguage","XPath"); 
             nodelist=xmlDoc.selectNodes("//info[contains(@keyword,'"+keyword1+"')]"); 
            if((keyword2!='')&&(nodelist.length<9))//第二個關鍵字不爲空且第一個關鍵字的文章少於9篇,
             nodelist1 =xmlDoc.selectNodes("//info[contains(@keyword,'"+keyword2+"')]");
          }
  
          var m=0;
          if(nodelist!=null&&nodelist1==null)
             m=nodelist.length;
           if(nodelist==null&&nodelist1!=null)
             m=nodelist1.length;
          if(nodelist!=null&&nodelist1!=null)
           m=nodelist.length+nodelist1.length;
        
          if((keyword2!=''&&m>2)||(keyword2==''&&m>1))//除去文章本身
          {
            str = '<div class=/"title tit fwb/">'+Relatedtitle+'</div><div class=/"end_Rlist p10/"><ul>' ;
           var n=0;
   
    if(nodelist!=null)
    {
    for (i=0;i<nodelist.length;i++)
  {
 if(n<8)
 {
       title1=nodelist[i].getAttribute("title");
       URL=nodelist[i].getAttribute("url"); 
          str+='<li><a href=/"'+URL+'/" target="/_blank/">'+title1+'</a></li>';
       n=n+1;  
       }
   //      alert(n);
    }
    }
    if(nodelist1!=null)
    {
    for (i=0;i<nodelist1.length;i++)
  {
 if(n<8)
 {
        title2=nodelist1[i].getAttribute("title");
        if(title1!=title2)
        {
           URL=nodelist1[i].getAttribute("url");
            str+='<li><a href=/"'+URL+'/" target="/_blank/">'+title2+'</a></li>';
        n=n+1;
      }
      }   
    }
   }
 // alert(n);
     str+='</ul></div>';
   }
}
//alert(str);
return str;
   
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章