这个学期前期写的一段代码,本来是想自己做一个XNA(XML聚合)的,由于种种原因,做了一部分就终止了,这是其中处理XML文档的一部分。

‘xml加载,其中 linkurl 为表示 XML 文档的 URL 的变量
Set http=Server.CreateObject(“Microsoft.XMLHTTP”)
http.Open “GET”,linkurl,False
http.send

Set xmlDoc=Server.CreateObject(“Microsoft.XMLDOM”)
xmlDoc.Async=False
xmlDoc.ValidateOnParse=False
xmlDoc.Load(http.ResponseXML)

‘xml结点设置

set root = xmlDoc.documentElement

‘根据结点选取数据,兼容XML 1.0规范和 XML 2.0规范
set nodeLis = root.getElementsByTagName(“item”)

‘对于XML文件中时间标签不同的兼容处理
nodeCount = nodeLis.length
dim t,itemnode,nodeFlag,x,nodeString,nodelist
nodeFlag=0
set nodelist = root.getElementsByTagName(“item”)
nodeString=”pubDate”
set itemnode = nodelist.nextNode()
t=itemnode.childnodes.length
for x=0 to t-1
if(StrComp(itemnode.childNodes(x).nodeName,nodeString)=0) then
nodeFlag=1
exit for
end if
next

For i=1 to nodeCount
set node = nodeLis.nextNode()

‘处理xml
dim pubDate,pubTime,pub,dateString,link,title,describ,today
dim dateArray
today=Date()
dateArray=Array(“Jan”,”Feb”,”Mar”,”Apr”,”May”,”June”,”July”,”Aug”,”Sep”,”Oct”,”Nov”,”Dec”)
title=node.selectSingleNode(“title”).text
link=node.selectSingleNode(“link”).text
describ=node.selectSingleNode(“description”).text

‘时间标签为 pubDate
if nodeFlag=1 then
dateString=node.selectSingleNode(“pubDate”).text
‘时间标签为 dc:date
else
dateString=node.selectSingleNode(“dc:date”).text
end if

‘对于时间格式不统一进行格式化
dateString=dateString+” “
pub=split(dateString,” “)
if(ubound(pub)<=2) then
pubDate=pub(0)
pubTime=pub(1)
else
for j=0 to 11
if(StrComp(pub(2),dateArray(j))=0) then
pub(2)=j+1
exit for
end if
Next
pubDate=pub(3)&”-“&pub(2)&”-“&pub(1)
pubTime=pub(4)
end if

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.