js在xml中根据日期使用xpath查询记录的方法

js在xml中根据日期使用xpath查询记录的方法

编者:李国帅

qq:9611153 微信lgs9611153

时间:2008-11-27 9:38

背景原因:

很久之前碰到的问题,觉得有意思分享一下。

 

 

解决方案:

 

<HTML>

<HEAD>

<TITLE>Example</TITLE>

 

</HEAD>

<BODY>

 

<SCRIPT LANGUAGE="JScript">

Date.prototype.Format = function (fmt) { //author: meizz

    var o = {

        "M+": this.getMonth() + 1, //月份

        "d+": this.getDate(), //

        "h+": this.getHours(), //小时

        "m+": this.getMinutes(), //

        "s+": this.getSeconds(), //

        "q+": Math.floor((this.getMonth() + 3) / 3), //季度

        "S": this.getMilliseconds() //毫秒

    };

    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));

    for (var k in o)

    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));

    return fmt;

}

 

//=================直接使用xpath=================

function test0()

{

     var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");

     var oNamedNodeMap;

     xmlDoc.async = false;

     xmlDoc.resolveExternals = false;

     //xmlDoc.load("lib/DebugLog.xml");

    xmlDoc.loadXML('<DebugLogSet><DebugLog Id="1" File="f:\\project\\tsclient\\config_info.cpp" Line="78" Date="2008-10-15 18:7:23" Type="1" Info="TestEvent!" /></DebugLogSet>');

    var currNode;

    xmlDoc.setProperty("SelectionLanguage", "XPath");

    //currNode = xmlDoc.documentElement.selectNodes("//*[@Line>'78']");//不需要&gt; &lt;

    currNodes = xmlDoc.documentElement.selectNodes("*[starts-with(@Date, \"2008-10-15\")]");

     currNode = xmlDoc.documentElement.selectNodes("*[contains(@Date, \"2008-10-15\")]");//XPath1.0 函数

   //currNodes = xmlDoc.documentElement.selectNodes("*[compare(@Date, \"2008-10-15\")=0]");//XPath2.0 函数

    alert(currNode.length);

    ////DebugLogSet[DebugLog[@Date>'2008-10-15 18:7:23' and @Date<'2008-10-15 20:0:0']]/text() //text()=标记值

}

 

//==================================

function getDatePath(sbdate,sedate)

{

//    var sbdate = "2008-10-15";

//    var sedate = "2008-10-15";

    var bdate=new Date(Date.parse(sbdate.replace(/-/g,"\/")));

    var edate=new Date(Date.parse(sedate.replace(/-/g,"\/")));

//     alert(bdate.valueOf());

//     alert(edate.valueOf());

//   alert(bdate==edate);//false

//  alert(bdate.valueOf()==edate.valueOf());//true

 

// alert(date.toDateString());

// date.setDate(date.getDate()+-2);

// alert(date.toDateString());

 

   var xpath = "*[ false ";

    while(bdate.valueOf()<=edate.valueOf())

   {

        var bdates = bdate.Format("yyyy-MM-dd");//bdate.toDateString()

        xpath += " or starts-with(@Date, \""+bdates+"\")";

        bdate.setDate(bdate.getDate()+1);

   }

   xpath += "]";

   alert(xpath);//"*[starts-with(@Date, \"2008-10-15\")]"

   return xpath;

}

//以下是测试用代码---------------------------------------------------------------------

function test1()

{

     var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");

     var oNamedNodeMap;

     xmlDoc.async = false;

     xmlDoc.resolveExternals = false;

     //xmlDoc.load("lib/DebugLog.xml");

    xmlDoc.loadXML('<DebugLogSet><DebugLog Id="1" File="f:\\project\\tsclient\\config_info.cpp" Line="78" Date="2008-10-15 18:7:23" Type="1" Info="TestEvent!" /></DebugLogSet>');

     var xpath = getDatePath("2008-10-15","2008-10-16");

     var currNodes;

     currNodes = xmlDoc.documentElement.selectNodes(xpath);//XPath1.0 函数

     alert(currNodes.length);

     if(currNodes.length>0){

        var sdate = currNodes.item(0).attributes[1].nodeValue;

        alert(sdate);

     }

}

</SCRIPT>

<BUTTON onclick="test1()">Click Me!</BUTTON>

 

 

 

</BODY>

</HTML>

 

 

 

结果

 

猜你喜欢

转载自blog.csdn.net/lgs790709/article/details/85767080