新写的一个xmlClass
新写的xmlClass,有些简陋,不过常用的功能都可以直接调用了。
<%
'==============================================================
' xmlClass v1.10.0617 by CatSeven
'==============================================================
' 文件:xmlClass.asp
' 功能:常用的XML处理
' 作者:苗启源(http://www.miaoqiyuan.cn)
'==============================================================
class xmlClass
Dim xmlobj
Public Sub Class_Initialize
set xmlobj = Server.CreateObject("Microsoft.XMLDOM")
End Sub
Public Sub Class_Terminate
set xmlobj = Nothing
End Sub
'功能:从文件加载XML
'f -> file 要保存的XML文件
' web://aaa.xml 根目录下的 myw3.xml
' path://myw3.xml 当前目录下的 myw3.xml
' E:/web/www/myw3.xml
' http://localhost/myw3.xml
Public Sub Load(byval f)
f = Mappath(f)
xmlobj.load f
End Sub
'功能:将当前的数据保存到XML问及那
'f -> file 要保存的XML文件
' web://aaa.xml 根目录下的 myw3.xml
' path://myw3.xml 当前目录下的 myw3.xml
' E:/web/www/myw3.xml
' http://localhost/myw3.xml
Public Sub Save(byval f)
f = Mappath(f)
xmlobj.save f
End Sub
'功能:通过标签获取节点列表
'参数:tag -> TagName
'返回:符合条件的节点列表
Public Function getTags(byval tag)
dim p
set p = xmlobj.getElementsByTagName(tag)
set getTags = p
End Function
'功能:通过xPath获取节点列表
'参数:str -> xpath 字符串
'返回:符合条件的节点列表
Public Function xPath(byval str)
dim p
set p = xmlobj.selectNodes(str)
set xPath = p
End Function
'功能:设置节点属性
'参数:obj -> 要设置属性的节点
' othervalue -> 属性值:比如 a=1&b=2 ==> <xxx a="1" b="2" />
Public Sub setNode(byref obj,byval othervalue)
dim valArr
valArr = split(othervalue,"&")
for i = 0 to ubound(valArr)
if instr(valArr(i),"=")>0 then
valDB = split(valArr(i),"=")
obj.setAttribute valDB(0),valDB(1)
end if
next
End Sub
'功能:设置节点属性
'参数:obj -> 节点列表,必须是一个列表,且只为列表中的第一项添加子节点。一般为:getTags,xPath返回的节点列表。
' xmlname -> 属性名。
' xmlvalue-> 属性值。
Public Sub setAttribute(byval obj,byval xmlname,byval xmlvalue)
for i = 0 to obj.length - 1
obj(i).setAttribute xmlname,xmlvalue
next
End Sub
'功能:删除当前节点
'参数:obj -> 删除当前节点
Public Sub Remove(byval obj)
if obj.length>0 then obj(0).parentNode.removeChild obj(0)
End Sub
'功能:添加一个新的节点
'参数:obj -> 节点列表,必须是一个列表,且只为列表中的第一项添加子节点。一般为:getTags,xPath返回的节点列表。
' nodename -> 节点名称(tagName)
' xmlname -> 索引属性。如果在当前文件中有节点名相同,且属性相同的节点,则不会新增节点
' valuearr -> 索引属性的值,必须是一个一元数组。
' othervalue -> 传递到setNode的属性,请参照 Public Sub setNode
Public Sub Append(byval obj,byval nodename,byval xmlname,byval valuearr,byval othervalue)
if obj.length<1 then Exit Sub
for i = 0 to ubound(valuearr)
if trim(valuearr(i))<>"" then
if xPath("//"&nodename&"[@"&xmlname&"='"&valuearr(i)&"']").length=0 then
set newNode = xmlobj.CreateElement(nodename)
newNode.setAttribute xmlname,valuearr(i)
setNode newNode,otherValue
obj(0).appendChild(newNode)
end if
end if
next
End Sub
'功能:获取节点属性列表
'参数:obj -> 节点列表,必须是一个列表,且只为列表中的第一项添加子节点。一般为:getTags,xPath返回的节点列表。
' xmlname -> 属性名。
'返回:一元数组,包含了所有的属性名
Public Function getAttribute(byval obj,byval xmlname)
dim Arr
redim Arr(obj.length - 1)
for i = 0 to obj.length - 1
Arr(i) = obj(i).getAttribute(xmlname)
next
getAttribute = Arr
End Function
'功能:获取文件的路径
'f -> file 要保存的XML文件
' web://aaa.xml 根目录下的 myw3.xml
' path://myw3.xml 当前目录下的 myw3.xml
' E:/web/www/myw3.xml
' http://localhost/myw3.xml
'返回:文件的路径
Private Function Mappath(byval f)
if instr(f,"web://")>0 then
f = replace(f,"web://","/")
f = server.mappath(f)
elseif instr(f,"path://")>0 then
f = replace(f,"path://","")
f = server.mappath(f)
end if
if left(LCase(f),7)<>"http://" then f = "file://" & f
Mappath = f
End Function
end class
%>