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等等語言,可以不用此函數而用它來對文檔進行控制.