直接生成XML的Google SiteMap的asp代码

作者:Francesco Passantino 来源:iteam5.net 时间:2007-08-17 13:44:00 

 

下面提供生成XML的Google SiteMap代码[ASP版本]。

这个代码是生成全站文件链接的地图:


<%
Server.ScriptTimeout=50000
' sitemap_gen.asp
' A simple script to automatically produce sitemaps for a webserver, in the Google Sitemap Protocol (GSP)
' by Francesco Passantino
' www.iteam5.net/francesco/sitemap
' v0.2 released 5 june 2005 (Listing a directory tree recursively improvement)
'
' BSD 2.0 license,
' http://www.opensource.org/licenses/bsd-license.php

session("server")="http://www.aspxhome.com" '你的域名
vDir = "/" '制作SiteMap的目录,相对目录(相对于根目录而言)


set objfso = CreateObject("Scripting.FileSystemObject")
root = Server.MapPath(vDir)

'response.ContentType = "text/xml"
'response.write "<?xml version='1.0' encoding='UTF-8'?>"
'response.write "<urlset xmlns='http://www.google.com/schemas/sitemap/0.84'>"

str = "<?xml version='1.0' encoding='UTF-8'?>" & vbcrlf
str = str & "<urlset xmlns='http://www.google.com/schemas/sitemap/0.84'>"
& vbcrlf

Set objFolder = objFSO.GetFolder(root)
'response.write getfilelink(objFolder.Path,objFolder.dateLastModified)
Set colFiles = objFolder.Files
For Each objFile In colFiles
'response.write getfilelink(objFile.Path,objfile.dateLastModified)
str = str & getfilelink(objFile.Path,objfile.dateLastModified) & vbcrlf
Next
ShowSubFolders(objFolder)

'response.write "</urlset>"
str = str & "</urlset>" & vbcrlf
set fso = nothing

Set objStream = Server.CreateObject("ADODB.Stream")
With objStream
'.Type = adTypeText
'.Mode = adModeReadWrite
.Open
.Charset = "utf-8"
.Position = objStream.Size
.WriteText=str
.SaveToFile server.mappath("/sitemap.xml"),2 '生成的XML文件名
.Close
End With

Set objStream = Nothing
If Not Err Then
Response.Write("<script>alert('成功生成站点地图!');history.back();</script>")
Response.End
End If

Sub ShowSubFolders(objFolder)
Set colFolders = objFolder.SubFolders
For Each objSubFolder In colFolders
if folderpermission(objSubFolder.Path) then
'response.write getfilelink(objSubFolder.Path,objSubFolder.dateLastModified)
str = str & getfilelink(objSubFolder.Path,objSubFolder.dateLastModified) & vbcrlf
Set colFiles = objSubFolder.Files
For Each objFile In colFiles
'response.write getfilelink(objFile.Path,objFile.dateLastModified)
str = str & getfilelink(objFile.Path,objFile.dateLastModified) & vbcrlf
Next
ShowSubFolders(objSubFolder)
end if
Next
End Sub


Function getfilelink(file,datafile)
file=replace(file,root,"")
file=replace(file,"\","/")
If FileExtensionIsBad(file) then Exit Function
if month(datafile)<10 then filedatem="0"
if day(datafile)<10 then filedated="0"
filedate=year(datafile)&"-"&filedatem&month(datafile)&"-"&filedated&day(datafile)
getfilelink = "<url><loc>"&server.htmlencode(session("server")&vDir&file)&"</loc><lastmod>"&filedate&"</lastmod><changefreq>daily</changefreq><priority>1.0</priority></url>"
Response.Flush
End Function


Function Folderpermission(pathName)

'需要过滤的目录(不列在SiteMap里面)
PathExclusion=Array("\temp","\_vti_cnf","_vti_pvt","_vti_log","cgi-bin","\admin","\edu")
Folderpermission =True
for each PathExcluded in PathExclusion
if instr(ucase(pathName),ucase(PathExcluded))>0 then
Folderpermission = False
exit for
end if
next
End Function


Function FileExtensionIsBad(sFileName)
Dim sFileExtension, bFileExtensionIsValid, sFileExt
'modify for your file extension (http://www.googleguide.com/file_type.html)
Extensions = Array("png","gif","jpg","jpeg","zip","pdf","ps","html","htm","php","wk1","wk2","wk3","wk4","wk5","wki","wks","wku","lwp","mw","xls","ppt","doc","swf","wks","wps","wdb","wri","rtf","ans","txt")
'设置列表的文件名,扩展名不在其中的话SiteMap则不会收录该扩展名的文件

if len(trim(sFileName)) = 0 then
FileExtensionIsBad = true
Exit Function
end if

sFileExtension = right(sFileName, len(sFileName) - instrrev(sFileName, "."))
bFileExtensionIsValid = false 'assume extension is bad
for each sFileExt in extensions
if ucase(sFileExt) = ucase(sFileExtension) then
bFileExtensionIsValid = True
exit for
end if
next
FileExtensionIsBad = not bFileExtensionIsValid
End Function
%>


标签:XML,SiteMap,Google
0
投稿

猜你喜欢

  • Python玩转Excel的读写改实例

    2022-01-27 19:59:47
  • Python列表和集合的效率大比拼

    2021-09-04 14:10:16
  • php实现文章评论系统

    2024-05-13 09:53:40
  • VSCode如何巧用正则表达式快速处理字符段

    2022-06-13 06:44:00
  • js+css实现select的美化效果

    2024-04-16 09:52:25
  • Python内存管理方式和垃圾回收算法解析

    2022-09-10 17:49:11
  • Javascript学习笔记之 函数篇(二) : this 的工作机制

    2024-05-11 10:23:57
  • Python基础篇之初识Python必看攻略

    2021-02-21 11:26:10
  • Python新手学习函数默认参数设置

    2021-08-18 03:50:35
  • Python+OpenCV实现分水岭分割算法的示例代码

    2021-01-11 05:01:38
  • python实现爬虫统计学校BBS男女比例之多线程爬虫(二)

    2021-05-10 03:08:18
  • 基于Python实现经典植物大战僵尸游戏

    2021-01-28 11:17:20
  • Python实现SVN的目录周期性备份实例

    2021-07-19 16:13:22
  • go语言中的return语句

    2024-05-28 15:22:09
  • 使用Python完成15位18位身份证的互转功能

    2021-11-30 03:04:30
  • 浅谈LogMiner的使用方法

    2009-02-28 11:12:00
  • Python3变量与基本数据类型用法实例分析

    2023-07-09 10:52:39
  • Python unittest框架操作实例解析

    2023-06-12 14:14:57
  • php 无限级 SelectTree 类

    2024-05-13 09:25:03
  • python scipy求解非线性方程的方法(fsolve/root)

    2022-01-06 15:46:00
  • asp之家 网络编程 m.aspxhome.com