转载请注明来源: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文件 的内容。