总结
豆包 AI 助手文章总结

vbs类天生xml文件

黄舟
发布: 2017-02-13 15:36:14
原创
2089人浏览过

          转载请注明来源:vbs类天生xml文件
有两文件:
objxml.asp:测试文件
clsxml.asp:vbs类文件
代码:

objXML.asp
<%@ Language=VBScript %><% Option Explicit %><!--#INCLUDE FILE='clsXML.asp'--><%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('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 & '<br>')NextSet objXML = Nothing
Response.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
'*********************************************************************' 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('<?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) 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%>
登录后复制

以上就是vbs类天生xml文件 的内容,更多相关内容请关注PHP中文网(www.php.cn)!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
豆包 AI 助手文章总结
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号