zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

一个asp版XMLDOM操作类

ASP 操作 一个 XMLDOM
2023-06-13 09:14:02 时间
<scriptlanguage="vbscript"runat="server">
"============================================================
"作者:做回自己
"时间:2005-3-15
============================================================
ClassXMLClass
PrivateobjXml
PrivatexmlDoc
PrivatexmlPath
"//============================================================
"<!--类初始化及注销时的事件-->
SubClass_initialize
SetobjXml=Server.CreateObject("MSXML2.DOMDocument")
objXml.preserveWhiteSpace=true
objXml.async=false
EndSub
SubClass_Terminate
SetobjXml=Nothing
EndSub
"//============================================================
"<!--建立一个新的XML文档-->
PublicFunctionCreateNew(sName)
SettmpNode=objXml.createElement(sName)
objXml.appendChild(tmpNode)
SetCreateNew=tmpNode
EndFunction
"<!--从外部读入XML文档-->
PublicFunctionOpenXml(sPath)
OpenXml=False
sPath=Server.MapPath(sPath)
"Response.Write(sPath)
xmlPath=sPath
IfobjXml.load(sPath)Then
SetxmlDoc=objXml.documentElement
OpenXml=True
EndIf
EndFunction
"<!--从外部读入XML字符串-->
PublicSubLoadXml(sStr)
objXml.loadXML(sStr)
SetxmlDoc=objXml.documentElement
EndSub
PublicSubInceptXml(xObj)
SetobjXml=xObj
SetxmlDoc=xObj.documentElement
EndSub
"//============================================================
"<!--新增一个节点-->
PublicFunctionAddNode(sNode,rNode)
"sNodeSTRING节点名称
"rNodeOBJECT增加节点的上级节点引用
"=============================================================
DimTmpNode
SetTmpNode=objXml.createElement(sNode)
rNode.appendChildTmpNode
SetAddNode=TmpNode
EndFunction
"<!--新增一个属性-->
PublicFunctionAddAttribute(sName,sValue,oNode)
"sNameSTRING属性名称
"sValueSTRING属性值
"oNodeOBJECT增加属性的对象
"=============================================================
oNode.setAttributesName,sValue
EndFunction
"<!--新增节点内容-->
PublicFunctionAddText(FStr,cdBool,oNode)
DimtmpText
IfcdBoolThen
SettmpText=objXml.createCDataSection(FStr)
Else
SettmpText=objXml.createTextNode(FStr)
EndIf
oNode.appendChildtmpText
EndFunction
"========================================================================================================
"<!--取得节点指定属性的值-->
PublicFunctionGetAtt(aName,oNode)
"aNameSTRING属性名称
"oNodeOBJECT节点引用
"=============================================================
dimtmpValue
tmpValue=oNode.getAttribute(aName)
GetAtt=tmpValue
EndFunction
"<!--取得节点名称-->
PublicFunctionGetNodeName(oNode)
"oNodeOBJECT节点引用
GetNodeName=oNode.nodeName
EndFunction
"<!--取得节点内容-->
PublicFunctionGetNodeText(oNode)
"oNodeOBJECT节点引用
GetNodeText=oNode.childNodes(0).nodeValue
EndFunction
"<!--取得节点类型-->
PublicFunctionGetNodeType(oNode)
"oNodeOBJECT节点引用
GetNodeType=oNode.nodeValue
EndFunction
"<!--查找节点名相同的所有节点-->
PublicFunctionFindNodes(sNode)
DimtmpNodes
SettmpNodes=objXml.getElementsByTagName(sNode)
SetFindNodes=tmpNodes
EndFunction
"<!--查打一个相同节点-->
PublicFunctionFindNode(sNode)
DimTmpNode
SetTmpNode=objXml.selectSingleNode(sNode)
SetFindNode=TmpNode
EndFunction
"<!--删除一个节点-->
PublicFunctionDelNode(sNode)
DimTmpNodes,Nodesss
SetTmpNodes=objXml.selectSingleNode(sNode)
SetNodesss=TmpNodes.parentNode
Nodesss.removeChild(TmpNodes)
EndFunction
"<!--替换一个节点-->
PublicFunctionReplaceNode(sNode,sText,cdBool)
"replaceChild
DimTmpNodes,tmpText
SetTmpNodes=objXml.selectSingleNode(sNode)
"AddTextsText,cdBool,TmpNodes
IfcdBoolThen
SettmpText=objXml.createCDataSection(sText)
Else
SettmpText=objXml.createTextNode(sText)
EndIf
TmpNodes.replaceChildtmpText,TmpNodes.firstChild
EndFunction

PrivateFunctionProcessingInstruction
"//--创建XML声明
DimobjPi
SetobjPi=objXML.createProcessingInstruction("xml","version="&chr(34)&"1.0"&chr(34)&"encoding="&chr(34)&"gb2312"&chr(34))
"//--把xml生命追加到xml文档
objXML.insertBeforeobjPi,objXML.childNodes(0)
EndFunction
"//=============================================================================
"<!--保存XML文档-->
PublicFunctionSaveXML()
"ProcessingInstruction()
objXml.save(xmlPath)
EndFunction
"<!--另存XML文档-->
PublicFunctionSaveAsXML(sPath)
ProcessingInstruction()
objXml.save(sPath)
EndFunction
"//==================================================================================
"相关统计
"<!--取得根节点-->
PropertyGetRoot
SetRoot=xmlDoc
EndProperty
"<!--取得根节点下子节点数-->
PropertyGetLength
Length=xmlDoc.childNodes.length
EndProperty
"//==================================================================================
"相关测试
PropertyGetTestNode
TestNode=xmlDoc.childNodes(0).text
EndProperty
EndClass
</script>