ASP XML operation class, friends who want to learn xml operation can refer to it. Copy the code. The code is as follows: 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
SubClass_Terminate
Set objXml = Nothing
End Sub
'//================================================ =============
'
Public Function CreateNew(sName)
Set tmpNode = objXml.createElement(sName)
objXml.appendChild(tmpNode)
Set CreateNew = tmpNode
End Function
'
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
'
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 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
'
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
'
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 attribute name
' oNode OBJECT node reference
'================================================== ============
dim tmpValue
tmpValue = oNode.getAttribute(aName)
GetAtt = tmpValue
End Function
'
Public Function GetNodeName(oNode)
' oNode OBJECT node reference
GetNodeName = oNode.nodeName
End Function
'
Public Function GetNodeText(oNode)
' oNode OBJECT node reference
GetNodeText = oNode.childNodes(0).nodeValue
End Function
'
Public Function GetNodeType(oNode)
' oNode OBJECT node reference
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)
DimTmpNode
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
'//--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
'//================================================ ==============================
'
Public Function SaveXML()
'ProcessingInstruction()
objXml.save(xmlPath)
End Function
'
Public Function SaveAsXML(sPath)
ProcessingInstruction()
objXml.save(sPath)
End Function
'//================================================ ===================================
'Related statistics
'
Property Get Root
Set Root = xmlDoc
End Property
'
Property Get Length
Length = xmlDoc.childNodes.length
End Property
'//================================================ ===================================
'Related tests
Property GetTestNode
TestNode = xmlDoc.childNodes(0).text
End Property
End Class
The main methods and implementation of ASP operating XML files on the server side through XMLDom
For small data volumes, xml files have many advantages over ACCESS in retrieval and update.
I once tested without using a database and stored the website's member information, product data information, transaction information, and website customization information in three xml files. The running results were very normal. It felt much faster than the database, but I didn't test it. Not sure.
Let’s talk about the main methods of creating, querying, modifying, etc. xml operations
'Create DOM object
set objDom=server.CreateObject(MicroSoft.XMLDom)
'Get xml data
'Method 1 Get the xml data of the xml file
objDom.load(c:/test.xml)
'Method 2 Get the data of xml data string
objDom.loadxml(<people><man name=sd/></people>)
'Create a node object
Set Newnode=objDom.CreateElement(people)
'Give this node a value
Newnode.Text=People
'Add attributes to this node
Set NewAttribute=objDom.CreateNode(attribute,name,)
NewAttribute.Text= Zhang San
Newnode.SetAttributeNode NewAttribute
'Add child nodes to this node
Set NewnodeChild=objDom.CreateElement(address)
Newnode.appendChild NewnodeChild
'Save this node object
objDom.appendChild Newnode
objDom.save(c:/test.xml)
'Find a node object
set objtofind=objdom.documentElement.SelectSingleNode(//people/man)
'Get the node name, node value, a certain attribute value, and all xml of this node object
nodename=objtofind.nodename
nodevalue=objtofind.text
objtofind.GetAttributeNode(name).Nodevalue 'Attribute value with attribute name name
'Get an attribute node object
set objattrtofind=objdom.documentElement.SelectSingleNode(//people/man).GetAttributeNode(name)
'Get the attribute name and attribute value of this node
nodeattrname=objattrtofind.nodename
nodeattrvalue=objattrtofind.nodevalue
'Delete a node object
set objnode=objdom.documentElement.SelectSingleNode(//people/man) 'The node to be deleted
set objparentnode=objdom.documentElement.SelectSingleNode(//people) 'The parent node of the node to be deleted
objparentnode.removeChild objnode
'Get the byte point set of a node
set objnodes=objdom.documentElement.SelectSingleNode(//people/man).ChildNodes
Iterate over this collection
Method 1
for each element in objnodes
response.write element.nodename byte naming
response.write element.text byte point value
next
Method 2
domlength=objnodes.length
for i = 0 to domlength-1
response.write objnodes.childnodes(i).nodename byte naming
response.write objnodes.childnodes(i).text byte point value
next
'Get the attribute set of a node
set objnodes=objdom.documentElement.SelectSingleNode(//people/man).GetAttributeNode(name).attributes
Iterate over this collection
for each element in objnodes
response.write element.nodename attribute name
response.write element.nodevalue attribute value
next
Once you are able to skillfully use the xmldom object to manipulate xml files, you can enjoy
The xmlhttp object is used to realize many functions under asp.
re:[Transfer]The main methods and implementation of ASP operating XML files on the server side through XMLDom
Feifei, why don’t you introduce the usage of @? Without this, many things will be inconvenient.
XML, some people also call it data compression technology, as the name suggests, XML can be used as a database.
Therefore, we can think of xml as a small database. Why is it called small? Because of the functions and application convenience of XML itself, there are still certain differences with databases. So why do we use xml? Because sometimes some of our applications perform data access, but if a database is used, the display is not flexible and convenient enough. At this time, we should use it in combination with xml.
Since xml can be regarded as a database, its first step is of course to create a link object. (Take ASP+xml as an example)
The creation method is the same as the link database, use server.createobject to create.
Here's how:
set xmlDoc= Server.CreateObject(microsoft.xmldom)
xmlDoc.async=false
xmldata=absolute path to data source
xmlDoc.load xmldata 'Use the load method to link here
Since the data format of xml is relatively user-friendly, the data format may be illegal due to human or other reasons. If you continue to use it at this time, it will cause the program to run out of process. In this way, we often do it after creating the link object. Data format validation.
Here's how:
if xmlDoc.parseError.errorCode<>0 then
….Error handling
<%
'----------
'Program introduction: Complete the asp language to add, delete, modify and view the text of the specified node in the XML document
'Entry parameters: None
'Export parameters: None
'--------
'Function name: ConnectXml()
'Entry parameters: filename xml file name to be connected or opened
'Export parameters: None
'Return value: ConnectXml=0, XMLMorntekDocument is an object that successfully loads the XML document.
'ConnectXml<>0, then print the error message strError
'--------
dim XMLMorntekDocument
function ConnectXml(filename)
dim strSourceFile
strSourceFile = Server.MapPath(filename)
Set XMLMorntekDocument = Server.CreateObject(Microsoft.XMLDOM)
XMLMorntekDocument.async = false
XMLMorntekDocument.load(strSourceFile)
ConnectXml=XMLMorntekDocument.parseerror.errorcode
if XMLMorntekDocument.parseerror.errorcode<>0 then
strError=<h2>error&XMLMorntekDocument.parseerror.errorcode&</h2>
strError=strError&XMLMorntekDocument.parseerror.reason&<br>
strError=strError&XMLMorntekDocument.parseerror.url&<br>
strError=strError&XMLMorntekDocument.parseerror.line&<br>
strError=strError&XMLMorntekDocument.parseerror.filepos&<br>
strError=strError&XMLMorntekDocument.parseerror.srcText&<br>
response.write strError
end if
end function
'--------
'Function name: CloseXml()
'Entry parameters: None
'Export parameters: None
'--------
function CloseXml(XMLMorntekDocument)
if IsObject(XMLMorntekDocument) then
set XMLMorntekDocument=nothing
end if
end function
'--------
'Function name: SelectXmlNodeText(elementname)
'Entry parameter: elementname The name of the element
'Export parameters: None
'--------
function SelectXmlNodeText(elementname)
elementname=//&elementname
temp=XMLMorntekDocument.selectSingleNode(elementname).text
selectXmlNodeText= server.htmlencode(temp)
end function
'--------
'Function name: InsertXmlNodeText(befelementname,elementname,elementtext)
'Entry parameter: elementname The name of the inserted element
' befelementname inserts an element before the name of this element
'elementtext The text of the inserted element
'Export parameters: None
'--------
function InsertXmlNodeText(befelementname,elementname,elementtext)
dim befelement,element
set befelement=XMLMorntekDocument.selectSingleNode(//&befelementname)
set element= XMLMorntekDocument.createelement(elementname)
befelement.insertBefore element,befelement.firstchild
element.text=elementtext
end function
'--------
'Function name: UpdateXmlNodeText(elementname,newelementtext)
'Entry parameter: elementname The name of the element
' The new text of the newelementtext element
'Export parameters: None
'--------
function UpdateXmlNodeText(elementname,newelementtext)
dim element
set element=XMLMorntekDocument.selectSingleNode(//&elementname)
element.text=newelementtext
end function
'--------
'Function name: DeleteXmlNodeText(elementname)
'Entry parameter: elementname The name of the element
'Export parameters: None
'--------
function DeleteXmlNodeText(elementname)
XMLMorntekDocument.selectSingleNode(//&elementname).text =
end function
%>
____________________
This article has not been tested and its feasibility is unknown.