一個asp版XMLDOM操作類
2024-05-04 11:00:34
供稿:網友
<script language="vbscript" runat="server">
'============================================================
'作者:做回自己
'時間:2005-3-15
============================================================
Class XMLClass
Private objXml
Private xmlDoc
Private xmlPath
'//============================================================
'<!--類初始化及注銷時的事件-->
Sub Class_initialize
Set objXml = Server.CreateObject("MSXML2.DOMDocument")
objXml.preserveWhiteSpace = true
objXml.async = false
End Sub
Sub Class_Terminate
Set objXml = Nothing
End Sub
'//============================================================
'<!--建立一個新的XML文檔-->
Public Function CreateNew(sName)
Set tmpNode = objXml.createElement(sName)
objXml.appendChild(tmpNode)
Set CreateNew = tmpNode
End Function
'<!--從外部讀入XML文檔-->
Public Function OpenXml(sPath)
OpenXml=False
sPath=Server.MapPath(sPath)
'Response.Write(sPath)
xmlPath = sPath
If objXml.load(sPath) Then
Set xmlDoc = objXml.documentElement
OpenXml=True
End If
End Function
'<!--從外部讀入XML字符串-->
Public Sub LoadXml(sStr)
objXml.loadXML(sStr)
Set xmlDoc = objXml.documentElement
End Sub
Public Sub InceptXml(xObj)
Set objXml = xObj
Set xmlDoc = xObj.documentElement
End Sub
'//============================================================
'<!--新增一個節點-->
Public Function AddNode(sNode,rNode)
' sNode STRING 節點名稱
' rNode OBJECT 增加節點的上級節點引用
'=============================================================
Dim TmpNode
Set TmpNode = objXml.createElement(sNode)
rNode.appendChild TmpNode
Set AddNode = TmpNode
End Function
'<!--新增一個屬性-->
Public Function AddAttribute(sName,sValue,oNode)
' sName STRING 屬性名稱
' sValue STRING 屬性值
' oNode OBJECT 增加屬性的對象
'=============================================================
oNode.setAttribute sName,sValue
End Function
'<!--新增節點內容-->
Public Function AddText(FStr,cdBool,oNode)
Dim tmpText
If cdBool Then
Set tmpText = objXml.createCDataSection(FStr)
Else
Set tmpText = objXml.createTextNode(FStr)
End If
oNode.appendChild tmpText
End Function
'========================================================================================================
'<!--取得節點指定屬性的值-->
Public Function GetAtt(aName,oNode)
' aName STRING 屬性名稱
' oNode OBJECT 節點引用
'=============================================================
dim tmpValue
tmpValue = oNode.getAttribute(aName)
GetAtt = tmpValue
End Function
'<!--取得節點名稱-->
Public Function GetNodeName(oNode)
' oNode OBJECT 節點引用
GetNodeName = oNode.nodeName
End Function