An xmldom operation code to facilitate code that needs to be combined with xml <script language=vbscript runat=server>
'================================================== ===========
'Author: Be yourself
'Time: 2005-3-15
================================================== ==========
Class XMLClass
Private objXml
Private xmlDoc
Private xmlPath
'//================================================ =============
'<!--Events during class initialization and logout-->
Sub Class_initialize
Set objXml = Server.CreateObject(MSXML2.DOMDocument)
objXml.preserveWhiteSpace = true
objXml.async = false
End Sub
SubClass_Terminate
Set objXml = Nothing
End Sub
'//================================================ =============
'<!--Create a new XML document-->
Public Function CreateNew(sName)
Set tmpNode = objXml.createElement(sName)
objXml.appendChild(tmpNode)
Set CreateNew = tmpNode
End Function
'<!--Read XML document from outside -->
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
'<!--Read XML string from outside -->
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
'//================================================ =============
'<!--Add a new node-->
Public Function AddNode(sNode,rNode)
' sNode STRING node name
'rNode OBJECT adds the node's superior node reference
'================================================== ============
DimTmpNode
Set TmpNode = objXml.createElement(sNode)
rNode.appendChild TmpNode
Set AddNode = TmpNode
End Function
'<!--Add a new attribute-->
Public Function AddAttribute(sName,sValue,oNode)
' sName STRING attribute name
' sValue STRING attribute value
' oNode OBJECT object with added attributes
'================================================== ============
oNode.setAttribute sName,sValue
End Function
'<!--Add node content-->
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
'================================================== ================================================== =====
'<!--Get the value of the specified attribute of the node-->
Public Function GetAtt(aName,oNode)
' aName STRING attribute name
' oNode OBJECT node reference
'================================================== ============
dim tmpValue
tmpValue = oNode.getAttribute(aName)
GetAtt = tmpValue
End Function
'<!--Get the node name-->
Public Function GetNodeName(oNode)
' oNode OBJECT node reference
GetNodeName = oNode.nodeName
End Function
'<!--Get node content-->
Public Function GetNodeText(oNode)
' oNode OBJECT node reference
GetNodeText = oNode.childNodes(0).nodeValue
End Function
'<!--Get node type-->
Public Function GetNodeType(oNode)
' oNode OBJECT node reference
GetNodeType = oNode.nodeValue
End Function
'<!--Find all nodes with the same node name-->
Public Function FindNodes(sNode)
Dim tmpNodes
Set tmpNodes = objXml.getElementsByTagName(sNode)
Set FindNodes = tmpNodes
End Function
'<!--Check for an identical node-->
Public Function FindNode(sNode)
DimTmpNode
Set TmpNode=objXml.selectSingleNode(sNode)
Set FindNode = TmpNode
End Function
'<!--Delete a node-->
Public Function DelNode(sNode)
Dim TmpNodes,Nodesss
Set TmpNodes=objXml.selectSingleNode(sNode)
Set Nodesss=TmpNodes.parentNode
Nodesss.removeChild(TmpNodes)
End Function
'<!--Replace a node-->
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
'//--Create XML declaration
Dim objPi
Set objPi = objXML.createProcessingInstruction(xml, version=&chr(34)&1.0&chr(34)& encoding=&chr(34)&gb2312&chr(34))
'//--Append xml life to xml document
objXML.insertBefore objPi, objXML.childNodes(0)
End Function
'//================================================ ==============================
'<!--Save XML document-->
Public Function SaveXML()
'ProcessingInstruction()
objXml.save(xmlPath)
End Function
'<!--Save XML document-->
Public Function SaveAsXML(sPath)
ProcessingInstruction()
objXml.save(sPath)
End Function
'//================================================ ===================================
'Related statistics
'<!--Get the root node-->
Property Get Root
Set Root = xmlDoc
End Property
'<!--Get the number of child nodes under the root node-->
Property Get Length
Length = xmlDoc.childNodes.length
End Property
'//================================================ ===================================
'Related tests
Property GetTestNode
TestNode = xmlDoc.childNodes(0).text
End Property
End Class
</script>