vbs類生成xml檔案(轉)
<!--#INCLUDE FILE="clsXML.asp"-->
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("Size", "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 of the nodes'returned by this XPath query.For Each str In objXML.getField("Jobs[@Size=24]")Response.Write(str & " Response.Redirect "New.xml"%> clsXML.asp:
'*********************************************************************' Initialization/Termination'********************************************************************* 'Initialize Class MembersPrivate Sub Class_Initialize()strFile = ""End Sub 'Terminate and unload all created objectsPrivate Sub Class_Terminate()Set objDoc = NothingEnd Sub '*********************************************************************' Properties'********************************************************************* 'Set XML File and objDocPublic Property Let File(str)Set objDoc = Server.CreateObject("Microsoft.XMLDOM")objDoc.async = FalsestrFile = strobjDoc.Load strFileEnd Property 'Get XML FilePublic Property Get File()File = strFileEnd Property '*********************************************************************' Functions'********************************************************************* '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("")objTextFile.WriteLine("")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) 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 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%>有兩檔案:objXML.asp:測試檔案clsXML.asp:vbs類檔案程式碼:objXML.asp
")NextSet objXML = Nothing
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-950349/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- FSO+遞迴生成檔案列表(xml) (轉)遞迴XML
- .VBS字尾檔案
- Python解析XML檔案生成HTMLPythonXMLHTML
- XML口令檔案描述 (轉)XML
- 如何用Shell指令碼生成XML檔案指令碼XML
- 檔案上傳用XML (轉)XML
- 使用XML上傳檔案 (轉)XML
- 利用dom4j來生成xml檔案XML
- 基於 DOM 的 XML 檔案解析類XML
- 使用XmlTextWriter物件建立XML檔案 (轉)XML物件
- 用php生成HTML檔案的類PHPHTML
- Java壓縮檔案生成工具類Java
- 透過XSL轉換XML檔案 (轉)XML
- 使用PHP DOM-XML建立和解析XML檔案 (轉)PHPXML
- xml檔案XML
- 使用XML幫助上傳檔案 (轉)XML
- XML檔案原始碼察看器(三) (轉)XML原始碼
- XML檔案原始碼察看器(四) (轉)XML原始碼
- XML檔案原始碼察看器(七) (轉)XML原始碼
- XML檔案原始碼察看器(二) (轉)XML原始碼
- XML檔案原始碼察看器(五) (轉)XML原始碼
- XML檔案原始碼察看器(六) (轉)XML原始碼
- 用php生成excel檔案 (轉)PHPExcel
- 巧用ASP生成PDF檔案 (轉)
- 用C#把檔案轉換為XML(轉)C#XML
- 自動生成檔案層級樹類
- Android程式除錯時生成main.out.xml檔案Android除錯AIXML
- 為 Windows 10/11 生成 autounattend.xml 檔案 (schneegans.de)WindowsXML
- [轉]XML檔案結構和基本語法XML
- 一個最簡單的XML檔案(轉)XML
- IDEA--生成POJO類及配置檔案IdeaPOJO
- Java讀取以.xlsx結尾的excel檔案,並寫出每張表對應的c#類、java類、儲存資料的xml檔案、讀取xml檔案的工具類JavaExcelC#XML
- 用VB將WORD文件(或其他的二進位制資料)生成xml檔案並互相轉換 (轉)XML
- SSM整合之使用配置類替換xml配置檔案(2)SSMXML
- jdom解析xml檔案XML
- [XML與properties檔案]XML
- GData解析XML檔案XML
- jquery 解析xml檔案jQueryXML