¹Ø¼ü´Ê£ºxml»º´æ,¸ßÐÔÄÜwebÕ¾µã,Êý¾Ý¿â¼õѹ,xml»º´æÀà
ÕýÎÄ£º
´òÔìÒ»¸ö¸ßÐÔÄÜÎȶ¨µÄwebÕ¾µãÒ»Ö±ÊÇÕ¾³¤ºÍ³ÌÐòÔ±µÄÃÎÏ룬±¾ÎÄÓÃ×÷ÕßµÄÒ»´ÎÇ×Éí¾ÀúµÄÀ´ËµËµÈçºÎÀûÓÃxml»º´æ¼¼ÊõʵÏÖÕ¾µãµÄ¸ßÐÔÄÜ¡£ÎÒÊÇ´Ó½ñÄ꿪ʼ×ö138ÊÖ»úÖ÷ÌâÍøµÄ£¬²ÉÓÃSQL2000×öΪÊý¾Ý¿â£¬¿ª·¢ÓïÑÔÓõÄÊÇAsp£¬²éѯµÄʱºò¶¼ÊǶ¯Ì¬²éѯ£¬Ö±½ÓÓÃlike %µÄ·½Ê½£¬ÄǸöʱºò·´ÕýÒ»ÌìµÄ·ÃÎÊÁ¿Ð¡£¬Í¬Ê±ÔÚÏßµÄʱºòÒ²¾Í¼¸Ê®¸öÈ˶øÒÑ£¬ËùÒÔ·þÎñÆ÷Ò²¾ÍÄÜʤÈÎÒªÇó£¬Ëæ×Å·ÃÎÊÁ¿ÂýÂýÔö¼Ó£¬µ±Í¬Ê±ÔÚÏß´ïµ½¼¸°ÙÈËʱ£¬´Ëʱ·þÎñÆ÷¿ªÊ¼²»¿°Öظº£¬CPU³£³£´ïµ½100%²»½µ£¬ÍøÒ³´ò¿ªËÙ¶ÈÒ²³¬¼¶Âý£¬Ò»¸ö²éѯҳÃæÐèÒª¼¸ÃëÖÓÉõÖÁ¸ü³¤£¬ÓÚÊÇÎÒ¿ªÊ¼¿¼ÂÇÓÅ»¯³ÌÐòºÍÊý¾Ý¿â£¬Êý¾Ý¿â½¨Á¢Ë÷Òý£¬²»ÊǺÜÀíÏ룬ÒòΪÓõÄÊÇlike '% ÕâÖÖ·½Ê½£¬ÓÚÊÇÎÒÏëµ½ÁË»º´æ£¬¶øxml±¾ÉíµÄÌصã¾ö¶¨ÁËËû·Ç³£ÊʺÏ×öÊý¾Ý¿âµÄ»º´æ£¬ºÃ¶«Î÷²»¸Ò¶ÀÏí£¬ÌØ·¢²¼³öÀ´£¬ÒÔ±ãͬÐн»Á÷£¬¹²Í¬½ø²½¡£
ʵÏÖµÄ˼·ÊÇÕâÑùµÄ£º³ÌÐò¶ÁÈ¡ÐÅϢʱ£¬ÏÈÅжÏÊÇ·ñ»º´æÁËxmlÊý¾Ý£¬Èç¹ûÓУ¬ÔòÖ±½Ó´ÓxmlÖжÁÈ¡ÐÅÏ¢£¬·ñÔò´ÓÊý¾Ý¿âÖжÁÈ¡£¬²¢½«´Ë´Î½á¹ûÉú³ÉxmlÎļþ£¬ÒÔ±ãÒÔºóµ÷Ó㬼ӿìËٶȣ¬Í¬Ê±ÅжÏxml»º´æÎļþÊÇ·ñ¹ýÆÚ£¬Èç¹û¹ýÆÚÔòÐèÒªÖØÐÂÉú³Éxml¡£ÏÂÃæÊǾßÌåµÄ´úÂë¡£
xmlcachecls.asp
<%
Rem xmlÊý¾Ý»º´æÀà
'--------------------------------------------------
'תÔصÄʱºòÇë±£Áô°æȨÐÅÏ¢
'×÷Õß:walkman
'ÍøÖ·:ÊÖ»úÖ÷Ìâ '°æ±¾:ver1.0
'»¶Ó¸÷λ½»Á÷½ø²½
'--------------------------------------------------
Class XmlCacheCls
Rem ˽ÓбäÁ¿¶¨Òå
Private m_CacheTime '»º´æʱ¼ä£¬µ¥Î»Ãë
Private m_PageSize 'ÿҳ´óС
Private m_CachePageNum 'xml»º´æÒ³´óС
Private m_XmlFile 'xml·¾¶£¬Óþø¶ÔµØÖ·,²»ÐèÒª¼ÓÀ©Õ¹Ãû
Private m_Sql 'SQLÓï¾ä
Private m_TableName '±íÃû»òÕßÊÓͼÃû
Private m_Columns 'ÁÐÃû ÓÃ,¸ô¿ª
Private m_CurPage 'µ±Ç°Ò³
Private m_CacheType '»º´æÀàÐÍ£º1,Áбí 2,ÏêÇé
Private m_DataConn 'Êý¾ÝÔ´,±ØÐëÒѾ´ò¿ª
Private m_QueryType '²éѯÀàÐÍ£º1,Ö±½ÓÓÃsql 2,Óô洢¹ý³Ì
Private m_SQLArr '·µ»ØµÄÊý¾ÝÊý×é
Private m_RecordCount
Rem ¹«¹²ÊôÐÔ
'»º´æʱ¼ä
Public Property Let CacheTime(v)
m_CacheTime = v
End Property
Public Property Get CacheTime
CacheTime = m_CacheTime
End Property
'ÿҳ´óС
Public Property Let PageSize(v)
m_PageSize = v
End Property
Public Property Get PageSize
PageSize = m_PageSize
End Property
'xml»º´æÒ³´óС
Public Property Let CachePageNum(v)
m_CachePageNum = v
End Property
Public Property Get CachePageNum
CachePageNum = m_CachePageNum
End Property
'xml·¾¶£¬Óþø¶ÔµØÖ·
Public Property Let XmlFile(v)
m_XmlFile = v
End Property
Public Property Get XmlFile
XmlFile = m_XmlFile
End Property
'xml·¾¶£¬Óþø¶ÔµØÖ·
Public Property Let Sql(v)
m_Sql = v
End Property
Public Property Get Sql
Sql = m_Sql
End Property
'±íÃû»òÕßÊÓͼÃû
Public Property Let TableName(v)
m_TableName = v
End Property
Public Property Get TableName
TableName = m_TableName
End Property
'ÁÐÃû ÓÃ,¸ô¿ª
Public Property Let Columns(v)
m_Columns = v
End Property
Public Property Get Columns
Columns = m_Columns
End Property
'µ±Ç°Ò³
Public Property Let CurPage(v)
m_CurPage = v
End Property
Public Property Get CurPage
CurPage = m_CurPage
End Property
'»º´æÀàÐÍ£º1,Áбí 2,ÏêÇé
Public Property Let CacheType(v)
m_CacheType = v
End Property
Public Property Get CacheType
CacheType = m_CacheType
End Property
'»º´æÀàÐÍ£º1,Áбí 2,ÏêÇé
Public Property Set Conn(v)
Set m_DataConn = v
End Property
Public Property Get Conn
Conn = m_DataConn
End Property
'·µ»Ø¼Ç¼×ÜÊý
Public Property Get RecordCount
RecordCount = m_RecordCount
End Property
'·µ»Ø¼Ç¼Êý×é
Public Property Get SQLArr
SQLArr = m_SQLArr
End Property
Rem ¹«¹²·½·¨ ¶ÁÈ¡Êý¾Ý
Public Function ReadData
If m_CacheType = 1 Then
ReadListAndSearchData
Else
ReadContentData
End If
End Function
Rem ¶ÁÈ¡ÏêÇéÐÅÏ¢
Private Function ReadContentData
Dim xmlfile
xmlfile = m_XmlFile
If FSOExistsFile(xmlfile) Then '´æÔÚxml»º´æ£¬Ö±½Ó´ÓxmlÖжÁÈ¡
ReadContentDataFromXml xmlfile
Else
ReadContentDataFromDB
End If
End Function
Rem ´ÓxmlÎļþ¶ÁÈ¡ÏêÇéÐÅÏ¢
Private Function ReadContentDataFromXml(xmlfile)
Dim SQLARR()
Dim XmlDoc
Set XmlDoc = Server.CreateObject("msxml2.FreeThreadedDOMDocument.3.0")
XmlDoc.Load xmlfile
Dim itemslength,itemsi
itemslength = XmlDoc.documentElement.childNodes.length
For itemsi=0 To itemslength-1
ReDim Preserve SQLARR(itemslength-1,0)
SQLARR(itemsi,0) = XmlDoc.documentElement.childNodes(itemsi).text
Next
Set XmlDoc = Nothing
m_SQLArr = SQLArr
End Function
Rem ´ÓDbÖжÁÈ¡ÏêÇéÐÅÏ¢
Private Function ReadContentDataFromDB()
Dim rs
Dim SQLARR
Set rs = m_DataConn.execute(m_sql)
IF Not Rs.eof Then
SQLArr=Rs.GetRows(1)
rs.close
Set rs = Nothing
Else
rs.close
Set rs = Nothing
Exit Function
End If
m_SQLArr = SQLArr
End Function
Rem ¶ÁÈ¡ÁбíÊý¾Ý
Private Function ReadListAndSearchData
Dim sPagesize,TotalPage,CurPage,TotalRec
sPagesize = m_PageSize * m_CachePageNum
m_CurPage = CLng(m_CurPage)
If m_CurPage Mod m_CachePageNum = 0 Then
CurPage = m_CurPage/m_CachePageNum
Else
CurPage = int(clng(m_CurPage)/m_CachePageNum)+1
End If
Dim xmlfile
xmlfile = getXmlFileName(CurPage)
If FSOExistsFile(xmlfile) Then '´æÔÚxml»º´æ£¬Ö±½Ó´ÓxmlÖжÁÈ¡
ReadListAndSearchDataFromXml xmlfile
Else
ReadListAndSearchDataFromDB
End If
End Function
Rem ´ÓxmlÖжÁÁбíÊý¾Ý
Private Function ReadListAndSearchDataFromXml(xmlfile)
Dim SQLARR()
Dim XmlDoc
Set XmlDoc = Server.CreateObject("msxml2.FreeThreadedDOMDocument.3.0")
XmlDoc.Load xmlfile
Dim totalrecont
totalrecont = XmlDoc.documentElement.selectSingleNode("totalrec").text
m_RecordCount = totalrecont
Dim TotalRec
TotalRec = m_RecordCount
If totalrecont = 0 Then
Set XmlDoc = Nothing
m_SQLArr = SQLARR
Exit Function
End If
Dim TotalPage,curpage
curpage = m_CurPage
If m_CurPage Mod m_CachePageNum = 0 Then
CurPage = m_CurPage/m_CachePageNum
Else
CurPage = int(clng(m_CurPage)/m_CachePageNum)+1
End If
If TotalRec Mod m_CachePageNum =0 Then
TotalPage = totalrecont/m_CachePageNum
Else
TotalPage = int(clng(totalrecont)/m_CachePageNum)+1
End If
If curpage>TotalPage Then curpage=TotalPage
Dim starti
Dim startn
startn = m_curpage - (curpage-1) * m_CachePageNum
Rem ¼ÆË㿪ʼλÖÃ
starti = (startn-1) * m_pagesize
Dim items,item
Set items = XmlDoc.documentElement.SelectNodes("item")
Dim i
Dim num
Dim length
length = items.length
num = 0
For i = starti To m_PageSize + starti -1
If i >=length Then Exit For
Set item = items(i)
Dim attrlength
attrlength = item.attributes.length
ReDim Preserve SQLARR(attrlength,num)
Dim Attribute
Dim Attributei
Attributei = 0
For Attributei = 0 To attrlength-1
SQLArr(Attributei,num) = item.attributes(Attributei).Nodevalue
Next
num = num + 1
Next
Set XmlDoc = Nothing
m_SQLArr = SQLArr
End Function
Rem ´ÓDBÖжÁÁбíÊý¾Ý
Private Function ReadListAndSearchDataFromDB
Dim rs,TotalRec,CurPage
CurPage = m_CurPage
Set Rs = Server.CreateObject("Adodb.Recordset")
Rs.open m_sql,m_DataConn,1
TotalRec = rs.recordcount
m_RecordCount = TotalRec
rs.pagesize = m_PageSize
If CurPage>rs.PageCount Then CurPage = rs.PageCount
If Not rs.eof Then rs.absolutePage=m_CurPage
Dim SQLARR()
Dim k
k = 0
While Not rs.eof and k<m_PageSize
Dim fieldlegth
fieldlegth = rs.Fields.count
ReDim Preserve SQLARR(fieldlegth,k)
Dim fieldi
For fieldi = 0 To fieldlegth-1
SQLArr(fieldi,k) = rs.Fields(fieldi).value
Next
rs.movenext
k=k+1
Wend
rs.close
Set rs = Nothing
m_SQLArr = SQLArr
End Function
Rem »ñÈ¡xmlÎļþÃû³Æ
Private Function getXmlFileName(num)
Dim tmpstr
tmpstr = LCase(m_XmlFile)
If Right(tmpstr,4) = ".xml" Then
tmpstr = Left(tmpstr,Len(tmpstr)-Len(".xml"))
End If
tmpstr = Replace(tmpstr,"%","_")
tmpstr = tmpstr & "_" & num & ".xml"
getXmlFileName = tmpstr
End Function
Rem ¹«¹²·½·¨ ½«Êý¾ÝдÈëxmlÎļþ
Public Function WriteDataToXml
If m_CacheType = 1 Then
WriteListAndSearchDataToXml
Else
WriteContentDataToXml
End If
End Function
Rem д¾ßÌåijÌõÐÅÏ¢µÄÏêÇéxml
Private Function WriteContentDataToXml
Rem xmlδ¹ýÆÚÔòÖ±½ÓÍ˳ö
Dim xmlfile
xmlfile = m_XmlFile
If FSOExistsFile(xmlfile) Then
If Not isXmlCacheExpired(xmlfile,m_CacheTime) Then Exit Function
End If
Dim rs
Set rs = Server.CreateObject("Adodb.Recordset")
Rs.open m_sql,m_DataConn
CreateContentXmlFile xmlfile,Rs
End Function
Rem ÁбíºÍËÑË÷xmlÊý¾Ý
Private Function WriteListAndSearchDataToXml
Dim sPagesize,TotalPage,CurPage,TotalRec
sPagesize = m_PageSize * m_CachePageNum
m_CurPage = CLng(m_CurPage)
If m_CurPage Mod m_CachePageNum = 0 Then
CurPage = m_CurPage/m_CachePageNum
Else
CurPage = int(clng(m_CurPage)/m_CachePageNum)+1
End If
Dim xmlfile
xmlfile = getXmlFileName(CurPage)
Rem Èç¹ûxmlδ¹ýÆÚÔòÖ±½ÓÍ˳ö
If FSOExistsFile(xmlfile) Then
If Not isXmlCacheExpired(xmlfile,m_CacheTime) Then Exit Function
End If
Dim rs
Set Rs = Server.CreateObject("Adodb.Recordset")
Rs.open m_sql,m_DataConn,1
TotalRec = rs.recordcount
rs.pagesize = sPagesize
If CurPage>rs.PageCount Then CurPage = rs.PageCount
CreateListAndSearchXMLFile xmlfile,TotalRec,Rs,sPagesize
End Function
Rem ˽Óз½·¨
Rem µÃµ½ÎļþµÄ×îºóÐÞ¸Äʱ¼ä
Private Function FSOGetFileLastModifiedTime(file)
Dim fso,f,s
Set fso=CreateObject("Scripting.FileSystemObject")
Set f=fso.GetFile(file)
FSOGetFileLastModifiedTime = f.DateLastModified
Set f = Nothing
Set fso = Nothing
End Function
Rem ÅжÏxml»º´æÊÇ·ñµ½ÆÚ
Private Function isXmlCacheExpired(file,seconds)
Dim filelasttime
filelasttime = FSOGetFileLastModifiedTime(file)
If DateAdd("s",seconds,filelasttime) < Now Then
isXmlCacheExpired = True
Else
isXmlCacheExpired = False
End If
End Function
Rem ÎļþÊÇ·ñ´æÔÚ
Private Function FSOExistsFile(file)
Dim fso
Set fso = Server.CreateObject("Scripting.FileSystemObject")
If fso.FileExists(file) Then
FSOExistsFile = true
Else
FSOExistsFile = false
End If
Set fso = nothing
End Function
Rem Éú³ÉÏêϸÊý¾ÝµÄxml
Private Function CreateContentXmlFile(xmlfile,Rs)
Dim xmlcontent
xmlcontent = "<?xml version=""1.0"" encoding=""gb2312""?>" & vbnewline
xmlcontent = xmlcontent & "<root>" & vbnewline
Dim field
For Each field In rs.Fields
xmlcontent = xmlcontent & "<"&field.name&">"
Dim value
value = field.value
If TypeName(value) = "String" Then
xmlcontent = xmlcontent & "<![CDATA[" & Trim(value) & "]]>"
Else
xmlcontent = xmlcontent & Trim(value)
End If
xmlcontent = xmlcontent & "</"&field.name&">" & vbnewline
Next
rs.close
Set rs = Nothing
xmlcontent = xmlcontent & "</root>" & vbnewline
Dim folderpath
folderpath = Trim(left(xmlfile,InstrRev(xmlfile,"\")-1))
Call CreateDIR(folderpath&"") '´´½¨Îļþ¼Ð
WriteStringToXMLFile xmlfile,xmlcontent
End Function
Rem Éú³ÉÁбíµÄxml
Private Function CreateListAndSearchXMLFile(xmlfile,TotalRec,Rs,sPagesize)
Dim xmlcontent
xmlcontent = ""
xmlcontent = xmlcontent & "<?xml version=""1.0"" encoding=""gb2312""?>" & vbnewline
xmlcontent = xmlcontent & " <root>" & vbnewline
xmlcontent = xmlcontent & " <totalrec>" & TotalRec & "</totalrec>" & vbnewline
Dim k
k = 0
Dim field
While Not rs.eof and k<sPagesize
xmlcontent = xmlcontent & " <item "
For Each field In rs.Fields
xmlcontent = xmlcontent & field.name & "=""" & XMLStringEnCode(field.value) & """ "
Next
xmlcontent = xmlcontent & "></item>" & vbnewline
rs.movenext
k=k+1
Wend
rs.close
Set rs = Nothing
xmlcontent = xmlcontent & " </root>" & vbnewline
Dim folderpath
folderpath = Trim(left(xmlfile,InstrRev(xmlfile,"\")-1))
Call CreateDIR(folderpath&"") '´´½¨Îļþ¼Ð
WriteStringToXMLFile xmlfile,xmlcontent
End Function
Rem xmlתÒå×Ö·û
Private Function XMLStringEnCode(str)
If str&"" = "" Then XMLStringEnCode="":Exit Function
str = Replace(str,"<","<")
str = Replace(str,">",">")
str = Replace(str,"'","'")
str = Replace(str,"""",""")
str = Replace(str,"&","&")
XMLStringEnCode = str
End Function
Rem дÎļþ
Private Sub WriteStringToXMLFile(filename,str)
'On Error Resume Next
Dim fs,ts
Set fs= createobject("scripting.filesystemobject")
If Not IsObject(fs) Then Exit Sub
Set ts=fs.OpenTextFile(filename,2,True)
ts.writeline(str)
ts.close
Set ts=Nothing
Set fs=Nothing
End Sub
Rem ´´½¨Îļþ¼Ð
Private function CreateDIR(byval LocalPath)
On Error Resume Next
Dim i,FileObject,patharr,path_level,pathtmp,cpath
LocalPath = Replace(LocalPath,"\","/")
Set FileObject = server.createobject("Scripting.FileSystemObject")
patharr = Split(LocalPath,"/")
path_level = UBound (patharr)
For i = 0 To path_level
If i=0 Then
pathtmp=patharr(0) & "/"
Else
pathtmp = pathtmp & patharr(i) & "/"
End If
cpath = left(pathtmp,len(pathtmp)-1)
If Not FileObject.FolderExists(cpath) Then
'Response.write cpath
FileObject.CreateFolder cpath
End If
Next
Set FileObject = Nothing
If err.number<>0 Then
CreateDIR = False
err.Clear
Else
CreateDIR = True
End If
End Function
End Class
%>
´ËÀà°üº¬Á½ÖÖ»º´æ·½Ê½£ºÒ»ÖÖÊÇ»ùÓÚÁÐ±í·½Ê½µÄ£¬Èç°´ÕÕij¸öÀà±ðÏÔʾÐÅÏ¢¡¢ËÑË÷ij¸ö¹Ø¼ü´Ê½øÐÐÏÔʾ£»ÁíÍâÒ»ÖÖÊÇÏêϸҳÃæµÄ»º´æ£¬ÈçÏÔʾ¾ßÌåµÄijƪÎÄÕ¡£
´ËÀàÓë¾ßÌåµÄÒµÎñÂß¼Î޹أ¬Ö»¸ºÔðxmlÊý¾ÝµÄ¶ÁÈ¡ºÍ´æ´¢£¬ÅжÏÊÇ·ñ»º´æ¹ýÆÚ¾ö¶¨ÊÇ·ñÐèÒª¸üлº´æ¡£°´ÕÕÈý²ã¹¹¼ÜģʽµÄ»°£¬Ëü´¦ÓÚÊý¾Ý·ÃÎʲ㡣