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等等语言,可以不用此函数而用它来对文档进行控制.