There are two files: objxml.asp: test file CLSXML.ASP: VBS class file code: objxml.asp
<% @Language = VBScript%> <% option explicit%> <% DIM Objxml, strpath, strset objxml = new CLSXML
StrPath = Server.mappath (".") & "/new.xml"
Objxml.createfile strpath, "root" or if using an existing XML file: 'Objxml.file = "c: /file.xml"
Objxml.createrootchild "Images"
'Here Only One Attribute IS Added To The Images / Image NodeObjxml.createChildNodeWattr "Images", "Image", "ID", "1" objxml.Updatefield "Images // Image [@ id = 1]", "super.gif "Objxml.createrootnodewattr" jobs ", array (" size "," length "," width "), _Array (24, 31, 30) Objxml.createrootnodewattr" jobs ", Array," Length "," Width " "), _Array (24, 30, 29) Objxml.createrootnodewattr" jobs ", Array (" size "," length "," width "), _Array (24, 31, 85)
'Notice That All Three Job Nodes Have Size 24, All of Those' Nodes Will Be UpdatedObjxml.Updatefield "Jobs [@ size = 24]", "24's"
'Notice that only two nodes have the specified XPath, hence' only two new child nodes will be addedobjXML.createChildNodeWAttr "Jobs [@ Size = 24 and @ Length = 31]", "Specs", _Array ( "Wood", "Metal "," Color "), _Array (" Cedar "," Aluminum "," Green ")
'It is always important to iterate through all this xpath Query.for Each Str in objxml.Getfield ("Jobs [@ size = 24]") response.write (str & "
") Nextset Objxml = NothingResponse.Redirect "new.xml"%>
CLSXML.ASP:
<% Class clsxml'strfile must be full Path to Document, IE C: /XML/Xmlfile.xml'objdoc is The XML ObjectPrivate Strfile, Objdoc
'********************************************************** ********************************************************************************************************************************************TION **************************************************
'Initialize Class Membersprivate Sub Class_Initialize () Strfile = "" End Sub
'Terminate and unload all created Objectsprivate sub coplass_terminate () set objdoc = nothingend subjDOC = NOTHINGEND SUB
'********************************************************** ************************************************************* ************************************************
'Set XML File and Objdocpublic Property Let File (STR) Set objDoc = Server.createObject ("Microsoft.xmLDom") Objdoc.async = falsestrfile = strobjdoc.load Strfilend Property
'Get XML FilePublic Property Get File () file = strfilend property
'********************************************************** *********************************************************************************************************************************TION ************************************************
'Create Blank XML File, set current obj File to newly created filePublic Function createFile (strPath, strRoot) Dim objFSO, objTextFileSet objFSO = Server.CreateObject ( "Scripting.FileSystemObject") Set objTextFile = objFSO.CreateTextFile (strPath, True) objTextFile. WriteLine (" Xml version =" "1.0" "?>") Objtextfile.writeline ("<" & strroot & "") objtextfile.closeme.file = strpathset objtextFile = Nothingset Objfso = Nothingend Function'get XML FIELD (s) based on XPath input from root nodePublic Function getField (strXPath) Dim objNodeList, arrResponse (), iSet objNodeList = objDoc.documentElement.selectNodes (strXPath) ReDim arrResponse (objNodeList.length) For i = 0 To objNodeList.length - 1arrResponse (i) = objNodelist.item (i) .TextNextGetField = arrresponseend function
'Update existing node (s) based on XPath specsPublic Function updateField (strXPath, strData) Dim objFieldFor Each objField In objDoc.documentElement.selectNodes (strXPath) objField.Text = strDataNextobjDoc.Save strFileSet objField = NothingupdateField = TrueEnd Function
'Create node directly under rootPublic Function createRootChild (strNode) Dim objChildSet objChild = objDoc.createNode (1, strNode, "") objDoc.documentElement.appendChild (objChild) objDoc.Save strFileSet objChild = NothingEnd Function
'Create a child node under root node with attributesPublic Function createRootNodeWAttr (strNode, attr, val) Dim objChild, objAttrSet objChild = objDoc.createNode (1, strNode, "") If IsArray (attr) And IsArray (val) ThenIf UBound (attr ) -Lbound (attr) <> ubound (val) -lbound (val) belte function functionelsedim iFri = lbound (attr) to Ubound (attr) set objattr = objdoc.createattribute (Attr (i)) Objchild.setttribute Attr (i) , val (i) NextEnd IfElseSet objAttr = objDoc.createAttribute (attr) objChild.setAttribute attr, valEnd IfobjDoc.documentElement.appendChild (objChild) objDoc.Save strFileSet objChild = NothingEnd Function'Create a child node under the specified XPath NodePublic Function createChildNode ( strXPath, strNode) Dim objParent, objChildFor Each objParent In objDoc.documentElement.selectNodes (strXPath) Set objChild = objDoc.createNode (1, strNode, "") objParent.appendChild (objChild) NextobjDoc.Save strFileSet objParent = NothingSet objChild = NothingEnd Function
'Create a child node (s) under the specified XPath Node with attributesPublic Function createChildNodeWAttr (strXPath, strNode, attr, val) Dim objParent, objChild, objAttrFor Each objParent In objDoc.documentElement.selectNodes (strXPath) Set objChild = objDoc.createNode ( 1. Strnode, "") IF Isarray (attr) and isarray (VAL) THENIF Ubound (attr) -lbound (attr) <> Ubound (VAL) -Lbound (VAL) THENEXIT FUNCTIONELSEDIM IFOR I = LBound (attr) to Ubound attr) Set objAttr = objDoc.createAttribute (attr (i)) objChild.SetAttribute attr (i), val (i) NextEnd IfElseSet objAttr = objDoc.createAttribute (attr) objChild.setAttribute attr, valEnd IfobjParent.appendChild (objChild) NextobjDoc. Save strFileSet objParent = NothingSet objChild = NothingEnd Function'Delete the node specified by the XPathPublic Function deleteNode (strXPath) Dim objOldFor Each objOld In objDoc.documentElement.selectNodes (strXPath) objDoc.documentElement.removeChild objOldNextobjDoc.Save strFileSet objOld = NothingEnd FunctionEnd Class% >