一個簡單的採用XML的線上人員統計類

來源:互聯網
上載者:User

class online
sub userState(uState)
dim userIP,xmlDoc,root,newUser,delNode,oTime
oTime=day(now())*24*60+hour(now())*60+minute(now())
set xmlDoc = server.createObject("Msxml2.DOMDocument")
xmlDoc.async = false
xmlDoc.load(Server.mappath(xmlUrl & "online.xml"))
set root=xmlDoc.SelectNodes ("//online/at[@oSessionID=''" & session.sessionID & "'' or @oUname=''" & myCname & "'' or oUip=''" & userIP & "'']")
if root.length=0 then
set newUser=xmlDoc.createElement("at")
xmlDoc.documentElement.appendChild(newUser)
newUser.setAttribute "oSessionID",session.sessionID
newUser.setAttribute "oUname",myCname
newUser.setAttribute "oUtype",myUtype
newUser.setAttribute "oUstate",uState
newUser.setAttribute "oUip",userIP
newUser.setAttribute "oUactiveTime",oTime
set newUser=nothing
else
root.item(0).attributes.item(0).value=session.sessionID
root.item(0).attributes.item(1).value=myCname
root.item(0).attributes.item(2).value=myUtype
root.item(0).attributes.item(3).value=uState
root.item(0).attributes.item(4).value=userIP
root.item(0).attributes.item(5).value=now()
end if
set delNode=XmlDoc.documentElement.selectSingleNode("//online/at[@oUactiveTime<''" & oTime + 20 & "'']")
root.reomveChild(delNode)
set delNode=nothing
set root=nothing
xmlDoc.save(server.mappath(xmlUrl & "online.xml"))
end sub

sub onlineCount()
dim uOnline,gOnline,root,xmlDoc
set xmlDoc = server.createObject("Msxml2.DOMDocument")
xmlDoc.async = false
xmlDoc.load(Server.mappath(xmlUrl & "online.xml"))
set root=xmlDoc.SelectNodes ("//online/at[@oUtype=''1'']")
uOnline=root.length
set root=xmlDoc.SelectNodes ("//online/at[@oUtype=''0'']")
gOnline=root.length
set root=nothing
set xmlDoc=nothing
response.write "線上:" & Gonline+Uonline & " 會員:" & Uonline & " 遊客:" & Gonline
end sub

function xChar(str)
xChar=str
xChar=replace(xChar,"""",""")
xChar=replace(xChar,"''","''")
xChar=replace(xChar,"<","<")
xChar=replace(xChar,">",">")
xChar=replace(xChar,"&","&")
end function
end class

雖然這段代碼很簡單但已函蓋了XMLDOM的建立,儲存,節點的增加,刪除,節點的屬性設定,DOM中利用XSL進行簡單查詢等等.

在線上人數比較多的網站,建議先用XSL進行索引,否則速度不堪想象.

文中oTime記錄使用者的最後啟用時間,由於XML的日期期資料處理比較複雜,比如要用DTD,XSL等等給代碼編寫和使用者使用造成不變,故這裡將其轉換為數字型.

而對線上的統計通過 選取的文檔的 length 來取,當然你也可以直接用XSL的count函數.

xChar是XML特殊字元處理函數,這裡沒有加上字元處理,使用時在字元型資料處理時請使用此函數.當然如果你非常熟悉DTD等等語言,可以不用此函數而用它來對文檔進行控制.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.