一個asp版XMLDOM操作類
更新時(shí)間:2008年10月15日 11:15:30 作者:
一個xmldom操作類代碼,方便需要跟xml結(jié)合的代碼
<script language="vbscript" runat="server">
'============================================================
'作者:做回自己
'時(shí)間:2005-3-15
============================================================
Class XMLClass
Private objXml
Private xmlDoc
Private xmlPath
'//============================================================
'<!--類初始化及注銷時(shí)的事件-->
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
'//============================================================
'<!--新增一個節(jié)點(diǎn)-->
Public Function AddNode(sNode,rNode)
' sNode STRING 節(jié)點(diǎn)名稱
' rNode OBJECT 增加節(jié)點(diǎn)的上級節(jié)點(diǎn)引用
'=============================================================
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
'<!--新增節(jié)點(diǎn)內(nèi)容-->
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
'========================================================================================================
'<!--取得節(jié)點(diǎn)指定屬性的值-->
Public Function GetAtt(aName,oNode)
' aName STRING 屬性名稱
' oNode OBJECT 節(jié)點(diǎn)引用
'=============================================================
dim tmpValue
tmpValue = oNode.getAttribute(aName)
GetAtt = tmpValue
End Function
'<!--取得節(jié)點(diǎn)名稱-->
Public Function GetNodeName(oNode)
' oNode OBJECT 節(jié)點(diǎn)引用
GetNodeName = oNode.nodeName
End Function
'<!--取得節(jié)點(diǎn)內(nèi)容-->
Public Function GetNodeText(oNode)
' oNode OBJECT 節(jié)點(diǎn)引用
GetNodeText = oNode.childNodes(0).nodeValue
End Function
'<!--取得節(jié)點(diǎn)類型-->
Public Function GetNodeType(oNode)
' oNode OBJECT 節(jié)點(diǎn)引用
GetNodeType = oNode.nodeValue
End Function
'<!--查找節(jié)點(diǎn)名相同的所有節(jié)點(diǎn)-->
Public Function FindNodes(sNode)
Dim tmpNodes
Set tmpNodes = objXml.getElementsByTagName(sNode)
Set FindNodes = tmpNodes
End Function
'<!--查打一個相同節(jié)點(diǎn)-->
Public Function FindNode(sNode)
Dim TmpNode
Set TmpNode=objXml.selectSingleNode(sNode)
Set FindNode = TmpNode
End Function
'<!--刪除一個節(jié)點(diǎn)-->
Public Function DelNode(sNode)
Dim TmpNodes,Nodesss
Set TmpNodes=objXml.selectSingleNode(sNode)
Set Nodesss=TmpNodes.parentNode
Nodesss.removeChild(TmpNodes)
End Function
'<!--替換一個節(jié)點(diǎn)-->
Public Function ReplaceNode(sNode,sText,cdBool)
'replaceChild
Dim TmpNodes,tmpText
Set TmpNodes=objXml.selectSingleNode(sNode)
'AddText sText,cdBool,TmpNodes
If cdBool Then
Set tmpText = objXml.createCDataSection(sText)
Else
Set tmpText = objXml.createTextNode(sText)
End If
TmpNodes.replaceChild tmpText,TmpNodes.firstChild
End Function
Private Function ProcessingInstruction
'//--創(chuàng)建XML聲明
Dim objPi
Set objPi = objXML.createProcessingInstruction("xml", "version="&chr(34)&"1.0"&chr(34)&" encoding="&chr(34)&"gb2312"&chr(34))
'//--把xml生命追加到xml文檔
objXML.insertBefore objPi, objXML.childNodes(0)
End Function
'//=============================================================================
'<!--保存XML文檔-->
Public Function SaveXML()
'ProcessingInstruction()
objXml.save(xmlPath)
End Function
'<!--另存XML文檔-->
Public Function SaveAsXML(sPath)
ProcessingInstruction()
objXml.save(sPath)
End Function
'//==================================================================================
'相關(guān)統(tǒng)計(jì)
'<!--取得根節(jié)點(diǎn)-->
Property Get Root
Set Root = xmlDoc
End Property
'<!--取得根節(jié)點(diǎn)下子節(jié)點(diǎn)數(shù)-->
Property Get Length
Length = xmlDoc.childNodes.length
End Property
'//==================================================================================
'相關(guān)測試
Property Get TestNode
TestNode = xmlDoc.childNodes(0).text
End Property
End Class
</script>
'============================================================
'作者:做回自己
'時(shí)間:2005-3-15
============================================================
Class XMLClass
Private objXml
Private xmlDoc
Private xmlPath
'//============================================================
'<!--類初始化及注銷時(shí)的事件-->
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
'//============================================================
'<!--新增一個節(jié)點(diǎn)-->
Public Function AddNode(sNode,rNode)
' sNode STRING 節(jié)點(diǎn)名稱
' rNode OBJECT 增加節(jié)點(diǎn)的上級節(jié)點(diǎn)引用
'=============================================================
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
'<!--新增節(jié)點(diǎn)內(nèi)容-->
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
'========================================================================================================
'<!--取得節(jié)點(diǎn)指定屬性的值-->
Public Function GetAtt(aName,oNode)
' aName STRING 屬性名稱
' oNode OBJECT 節(jié)點(diǎn)引用
'=============================================================
dim tmpValue
tmpValue = oNode.getAttribute(aName)
GetAtt = tmpValue
End Function
'<!--取得節(jié)點(diǎn)名稱-->
Public Function GetNodeName(oNode)
' oNode OBJECT 節(jié)點(diǎn)引用
GetNodeName = oNode.nodeName
End Function
'<!--取得節(jié)點(diǎn)內(nèi)容-->
Public Function GetNodeText(oNode)
' oNode OBJECT 節(jié)點(diǎn)引用
GetNodeText = oNode.childNodes(0).nodeValue
End Function
'<!--取得節(jié)點(diǎn)類型-->
Public Function GetNodeType(oNode)
' oNode OBJECT 節(jié)點(diǎn)引用
GetNodeType = oNode.nodeValue
End Function
'<!--查找節(jié)點(diǎn)名相同的所有節(jié)點(diǎn)-->
Public Function FindNodes(sNode)
Dim tmpNodes
Set tmpNodes = objXml.getElementsByTagName(sNode)
Set FindNodes = tmpNodes
End Function
'<!--查打一個相同節(jié)點(diǎn)-->
Public Function FindNode(sNode)
Dim TmpNode
Set TmpNode=objXml.selectSingleNode(sNode)
Set FindNode = TmpNode
End Function
'<!--刪除一個節(jié)點(diǎn)-->
Public Function DelNode(sNode)
Dim TmpNodes,Nodesss
Set TmpNodes=objXml.selectSingleNode(sNode)
Set Nodesss=TmpNodes.parentNode
Nodesss.removeChild(TmpNodes)
End Function
'<!--替換一個節(jié)點(diǎn)-->
Public Function ReplaceNode(sNode,sText,cdBool)
'replaceChild
Dim TmpNodes,tmpText
Set TmpNodes=objXml.selectSingleNode(sNode)
'AddText sText,cdBool,TmpNodes
If cdBool Then
Set tmpText = objXml.createCDataSection(sText)
Else
Set tmpText = objXml.createTextNode(sText)
End If
TmpNodes.replaceChild tmpText,TmpNodes.firstChild
End Function
Private Function ProcessingInstruction
'//--創(chuàng)建XML聲明
Dim objPi
Set objPi = objXML.createProcessingInstruction("xml", "version="&chr(34)&"1.0"&chr(34)&" encoding="&chr(34)&"gb2312"&chr(34))
'//--把xml生命追加到xml文檔
objXML.insertBefore objPi, objXML.childNodes(0)
End Function
'//=============================================================================
'<!--保存XML文檔-->
Public Function SaveXML()
'ProcessingInstruction()
objXml.save(xmlPath)
End Function
'<!--另存XML文檔-->
Public Function SaveAsXML(sPath)
ProcessingInstruction()
objXml.save(sPath)
End Function
'//==================================================================================
'相關(guān)統(tǒng)計(jì)
'<!--取得根節(jié)點(diǎn)-->
Property Get Root
Set Root = xmlDoc
End Property
'<!--取得根節(jié)點(diǎn)下子節(jié)點(diǎn)數(shù)-->
Property Get Length
Length = xmlDoc.childNodes.length
End Property
'//==================================================================================
'相關(guān)測試
Property Get TestNode
TestNode = xmlDoc.childNodes(0).text
End Property
End Class
</script>
相關(guān)文章
ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享
本文介紹的是ASP動態(tài)網(wǎng)頁制作技術(shù)的一些經(jīng)驗(yàn)心得,主要從四個大的方面,給大家介紹的,需要的朋友可以參考下2015-10-10asp 讀取通過表單發(fā)送的post數(shù)據(jù)
學(xué)習(xí)ASP,最重要的就是要掌握ASP內(nèi)置的六大對象,如果以前沒接觸過,聰明的您就不要管這些概念了,知道怎么用就行了,我的觀點(diǎn)是剛開始關(guān)鍵在于臨摹2012-12-12asp獲取當(dāng)前完整路徑(url)的函數(shù)代碼
這篇文章主要介紹了asp獲取當(dāng)前完整路徑(url)的函數(shù)代碼,有時(shí)候我么您需要獲取網(wǎng)址,端口、路徑文件名、參數(shù)等,這里就為大家分享一下這個函數(shù)代碼,需要的朋友可以參考下2020-02-02