一个asp版XMLDOM操作类
(编辑:jimmy 日期: 2024/12/29 浏览:3 次 )
<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
'<!--取得节点内容-->
Public Function GetNodeText(oNode)
' oNode OBJECT 节点引用
GetNodeText = oNode.childNodes(0).nodeValue
End Function
'<!--取得节点类型-->
Public Function GetNodeType(oNode)
' oNode OBJECT 节点引用
GetNodeType = oNode.nodeValue
End Function
'<!--查找节点名相同的所有节点-->
Public Function FindNodes(sNode)
Dim tmpNodes
Set tmpNodes = objXml.getElementsByTagName(sNode)
Set FindNodes = tmpNodes
End Function
'<!--查打一个相同节点-->
Public Function FindNode(sNode)
Dim TmpNode
Set TmpNode=objXml.selectSingleNode(sNode)
Set FindNode = TmpNode
End Function
'<!--删除一个节点-->
Public Function DelNode(sNode)
Dim TmpNodes,Nodesss
Set TmpNodes=objXml.selectSingleNode(sNode)
Set Nodesss=TmpNodes.parentNode
Nodesss.removeChild(TmpNodes)
End Function
'<!--替换一个节点-->
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
'//--创建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
'//==================================================================================
'相关统计
'<!--取得根节点-->
Property Get Root
Set Root = xmlDoc
End Property
'<!--取得根节点下子节点数-->
Property Get Length
Length = xmlDoc.childNodes.length
End Property
'//==================================================================================
'相关测试
Property Get TestNode
TestNode = xmlDoc.childNodes(0).text
End Property
End Class
</script>
'============================================================
'作者:做回自己
'时间: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
'<!--取得节点内容-->
Public Function GetNodeText(oNode)
' oNode OBJECT 节点引用
GetNodeText = oNode.childNodes(0).nodeValue
End Function
'<!--取得节点类型-->
Public Function GetNodeType(oNode)
' oNode OBJECT 节点引用
GetNodeType = oNode.nodeValue
End Function
'<!--查找节点名相同的所有节点-->
Public Function FindNodes(sNode)
Dim tmpNodes
Set tmpNodes = objXml.getElementsByTagName(sNode)
Set FindNodes = tmpNodes
End Function
'<!--查打一个相同节点-->
Public Function FindNode(sNode)
Dim TmpNode
Set TmpNode=objXml.selectSingleNode(sNode)
Set FindNode = TmpNode
End Function
'<!--删除一个节点-->
Public Function DelNode(sNode)
Dim TmpNodes,Nodesss
Set TmpNodes=objXml.selectSingleNode(sNode)
Set Nodesss=TmpNodes.parentNode
Nodesss.removeChild(TmpNodes)
End Function
'<!--替换一个节点-->
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
'//--创建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
'//==================================================================================
'相关统计
'<!--取得根节点-->
Property Get Root
Set Root = xmlDoc
End Property
'<!--取得根节点下子节点数-->
Property Get Length
Length = xmlDoc.childNodes.length
End Property
'//==================================================================================
'相关测试
Property Get TestNode
TestNode = xmlDoc.childNodes(0).text
End Property
End Class
</script>
下一篇:asp下查询xml的实现代码