📄 function.asp
字号:
<%
Dim objXML, loadResult
Set objXML = Server.CreateObject("Msxml2.DOMDocument")
objXML.async = False
loadResult = objXML.load(Server.MapPath("bus.xml"))
If Not loadResult Then
Response.Write ("加载XML文件出错!")
Response.End
End If
'getBusidList
'得到所有车次的列表
Function getBusidList()
Dim objNodes
Dim i
Set objNodes = objXML.getElementsByTagName("公交车信息/bus")
ReDim temp(objNodes.length - 1)
For i = 0 To objNodes.length - 1
temp(i) = objNodes.Item(i).getAttribute("busid")
Next
getBusidList = temp
End Function
'getBusStateList
'得到所有站点的列表
Function getBusStateList()
Dim objNodes
Dim i, j, k, l, strTemp, flag
k = 0
l = 0
Set objNodes = objXML.getElementsByTagName("公交车信息/bus/各站名称/站名")
k = objNodes.length
ReDim temp(k)
ReDim temp1(k)
'将所有站点取出
For i = 0 To k - 1
temp(i) = objNodes.Item(i).Text
Next
'去掉重复项
temp1(0) = temp(0)
For i = 1 To k - 1
flag = 0 '重复项标志
For j = 0 To i - 1
If temp(j) = temp(i) Then
'重复项
flag = 1
Exit For
End If
Next
If flag = 0 Then ' 和前面的比较没有重复
l = l + 1
temp1(l) = temp(i)
End If
Next
'将非空值读到另外一个数组中
ReDim temp2(l)
For i = 0 To l
temp2(i) = temp1(i)
Next
'站点排序
For i = 1 To l
For j = 0 To i - 1
If Asc(temp2(j)) > Asc(temp2(i)) Then
strTemp = temp2(i)
temp2(i) = temp2(j)
temp2(j) = strTemp
End If
Next
Next
getBusStateList = temp2
End Function
'getInfoByBusID
'得到指定车次的详细信息
Function getInfoByBusID(busID)
busID = CStr(busID)
Dim objNodes
Dim k, l
Dim i, j
Set objNodes = objXML.getElementsByTagName("公交车信息/bus")
k = objNodes.length
For i = 0 To k - 1
If busID = objNodes.Item(i).getAttribute("busid") Then
j = objNodes.Item(i).childNodes.Item(3).childNodes.length '站点的数量
ReDim arrTemp(j + 2)
arrTemp(0) = objNodes.Item(i).childNodes.Item(0).Text '车次
arrTemp(1) = objNodes.Item(i).childNodes.Item(1).Text '起点站
arrTemp(2) = objNodes.Item(i).childNodes.Item(2).Text '终点站
For l = 0 To j - 1
'读各个站点名称
arrTemp(l + 3) = objNodes.Item(i).childNodes.Item(3).childNodes.Item(l).Text
Next
Exit For
End If
Next
getInfoByBusID = arrTemp
End Function
'getBusIDInfoByStation
'得到经过指点站点的车次列表
Function getBusIDInfoByStation(Station)
Dim busIDList
Dim busInfo
Dim i, j
Dim tempStr
tempStr = ""
busIDList = getBusidList
For i = 0 To UBound(busIDList)
busInfo = getInfoByBusID(busIDList(i))
For j = 3 To UBound(busInfo)
If busInfo(j) = Station Then
tempStr = tempStr + CStr(busIDList(i)) + ";"
Exit For
End If
Next
Next
tempStr = Left(tempStr, Len(tempStr) - 1) '去掉最后一个分号
getBusIDInfoByStation = Split(tempStr, ";")
End Function
'isPassTheStation
'判断指定车次是否经过指定的站点
Function isPassTheStation(Station, BusID)
Dim busInfo
Dim i, Result
Result = False
busInfo = getInfoByBusID(BusID)
For i = 3 To UBound(busInfo)
If busInfo(i) = Station Then
Result = True
Exit For
End If
Next
isPassTheStation = Result
End Function
'addBus
'增加一趟车次信息
Function addBus(arrBusInfo)
'返回值 True填加成功,False 填加失败(有重复名车次)
'arrBusInfo 的数据格式为
'arrBusInfo(0) 车次
'arrBusInfo(1)-arrBusInfo(...)各站名称
Dim objNode
Dim busIdList, i, busInfoNode, busIdInfo
Dim startState, endState
Dim startNode, endNode, busIdNode
Dim eachNode0
busIdList = getBusidList
For i = 0 To UBound(busIdList)
If busIdList(i) = arrBusInfo(0) Then
addBus = False '填加失败,因为有相同的车次名称
Exit Function
End If
Next
Set objNode = objXML.getElementsByTagName("公交车信息")
Set objNode = objNode.Item(0)
'创建一个新元素
Set busInfoNode = objXML.createNode("element", "bus", "")
'把元素内容添到XML中
Set busInfoNode = objNode.appendChild(busInfoNode)
'把车次写进bus的属性中
Set busIdInfo = objXML.createAttribute("busid")
busIdInfo.Text = arrBusInfo(0)
busInfoNode.Attributes.setNamedItem (busIdInfo)
'读出起点站和终点站
startState = arrBusInfo(1)
endState = arrBusInfo(UBound(arrBusInfo))
Set busIdNode = objXML.createNode("element", "车次", "")
busIdNode.Text = arrBusInfo(0)
Set busIdNode = busInfoNode.appendChild(busIdNode)
Set startNode = objXML.createNode("element", "起点站", "")
startNode.Text = startState
Set startNode = busInfoNode.appendChild(startNode)
Set endNode = objXML.createNode("element", "终点站", "")
endNode.Text = endState
Set endNode = busInfoNode.appendChild(endNode)
ReDim eachNode(UBound(arrBusInfo))
Set eachNode0 = objXML.createNode("element", "各站名称", "")
Set eachNode0 = busInfoNode.appendChild(eachNode0)
For i = 1 To UBound(arrBusInfo)
'arrBusInfo(1) 开始记录各个站点信息arrBusInfo(0)记录的是车次
Set eachNode(i) = objXML.createNode("element", "站名", "")
eachNode(i).Text = arrBusInfo(i)
Set eachNode(i) = eachNode0.appendChild(eachNode(i))
Next
objXML.save (Server.MapPath("bus.xml"))
addBus = True
End Function
'delBus
'删除指定车次
Function delBus(busID)
'返回值True 删除成功,False,失败,因为提供的车次不存在
Dim busIdList, i, flag
Dim busNode
busID = CStr(busID)
busIdList = getBusidList
flag = False
For i = 0 To UBound(busIdList)
If busID = busIdList(i) Then
flag = True
Exit For
End If
Next
If Not flag Then
'Response.Write ("删除的车次不存在!")
'Response.End
delBus = False
Exit Function
End If
Set busNode = objXML.getElementsByTagName("公交车信息/bus")
For i = 0 To busNode.length - 1
If busNode.Item(i).getAttribute("busid") = busID Then
busNode.Item(i).parentNode.removeChild busNode.Item(i)
Exit For
End If
Next
objXML.save (Server.MapPath("bus.xml"))
delBus = True
End Function
'getBusIndexByBusID
'由车次名称得到车次索引号
function getBusIndexByBusID(BusID)
dim i,busIDList
busIDList=getBusIDList
for i=0 to ubound(busIDList)
if busIDList(i)=BusID then
getBusIndexByBusID=i
exit function
end if
next
end function
%>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -