用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;
   
}

你可能感兴趣的:(用js实现xml模糊查找)