将信息保存到一个XML文件的vbs代码
程序员文章站
2022-04-10 08:17:08
this script demonstrates how to save information to a xml file with the use of msxml....
this script demonstrates how to save information to a xml file with the use of msxml. the example xml file is used for the photo album on the site.
'*****************************************************************
'** script: createxml.vbs
'** version: 1.0
'** created: 01/12/2009 9:51pm
'** author: adriaan westra
'** e-mail:
'** purpose / comments:
'** create xml file for photo album
'**
'**
'** changelog :
'** 12-01-2009 9:51 : initial version
'**
'*****************************************************************
on error resume next
dim version : version = "1.0" ' script version
dim author : author = "a. westra"
dim objxml 'xml document object
dim root 'root element of the xml document
dim newnode ' xml node object
dim cnode ' xml (child) node object
dim cnodetext ' xml text node object
'*****************************************************************
'** make sure the script is started with cscript
if instr(wscript.fullname, "wscript.exe") > 0 then
msgbox "please run this script with cscript.exe." & chr(13) & _
"for example : cscript " & wscript.scriptname & " /?", _
vbexclamation, wscript.scriptname
wscript.quit(1)
end if
'*****************************************************************
'** get commandline parameters
set args = wscript.arguments
if args.count = 0 then
strimagedir = inputbox("please give the directory name " & _
"to process : ",wscript.scriptname, strpath)
else
if instr(args(0),"/?") > 0 or instr(ucase(args(0)),"/h") > 0 _
or instr(ucase(args(0)),"/help") > 0 then
displayhelp
wscript.quit(0)
else
strimagedir = args(0)
end if
end if
set objxml = createobject("msxml2.domdocument.6.0")
objxml.setproperty "selectionlanguage", "xpath"
'*****************************************************************
'** determine if the file exists
strxmlfile = strimagedir & "\album.xml"
set objfso = createobject("scripting.filesystemobject")
if objfso.fileexists(strxmlfile) then
'*****************************************************************
'** read the xml file
objxml.load(strxmlfile)
else
'*****************************************************************
'** create the xml file
objxml.loadxml("")
end if
'*****************************************************************
'** process directory
set objimgdir = objfso.getfolder(strimagedir)
for each objfile in objimgdir.files
if isjpg(objfile.name) then
arrtemp = split(objfile.name, ".")
strnode = arrtemp(0)
'*****************************************************************
'** determine if the node exists
if not xmlnodeexists(strchildnode, objxml) then
'*****************************************************************
'** get the root element of the xml document
set root = objxml.documentelement
'*****************************************************************
'** create the new node
set newnode = objxml.createnode(1, strnode, "")
root.appendchild newnode
set cnode = objxml.createnode(1, "alt", "")
set cnodetext = objxml.createnode(3, "", "")
cnodetext.text = strnode
cnode.appendchild cnodetext
newnode.appendchild cnode
set cnode = objxml.createnode(1, "title", "")
set cnodetext = objxml.createnode(3, "", "")
cnodetext.text = strnode
cnode.appendchild cnodetext
newnode.appendchild cnode
end if
end if
next
'*****************************************************************
'** save the xml file
objxml.save(strxmlfile)
'*****************************************************************
'** end the script
wscript.quit
'*****************************************************************
'** function: xmlnodeexists
'** version: 1.0
'** created: 1/12/2009 12:14pm
'** author: adriaan westra
'** e-mail:
'**
'** purpose / comments:
'** determines if a node exists in xml
'**
'** arguments :
'** strnode :name of the xml node
'** oxml :xml dom object
'**
'** changelog :
'** 1/12/2009 12:16pm : initial version
'**
'*****************************************************************
function xmlnodeexists( strnode, oxml )
on error resume next
set onode = oxml.selectsinglenode(strnode)
strnodetype = onode.nodetype
if err.number = 0 then
xmlnodeexists = true
else
xmlnodeexists = false
end if
end function
'*****************************************************************
'** sub: displayhelp
'** version: 1.0
'** created: 24-03-2003 8:22
'** author: adriaan westra
'** e-mail:
'**
'** purpose / comments:
'** display help for script
'**
'** arguments :
'**
'** wijzigingslog :
'** 24-03-2003 8:22 : initi雔e versie
'**
'*****************************************************************
sub displayhelp()
strcomment = string(2,"*")
strcmntline = string(79, "*")
wscript.echo strcmntline
wscript.echo strcomment
wscript.echo strcomment & " online help for " & _
wscript.scriptname & " version : " & version
wscript.echo strcomment
wscript.echo strcomment & " usage : cscript " & _
wscript.scriptname & " directoryname"
wscript.echo strcomment
wscript.echo strcomment & " purpose : create xml file " & _
"for all images in given directory."
wscript.echo strcomment
wscript.echo strcomment & " author : " & author
wscript.echo strcomment & " e-mail : " & email
wscript.echo strcomment
wscript.echo strcmntline
end sub
'*****************************************************************
'** function: isjpg
'** version: 1.0
'** created: 12/29/2008 11:01pm
'** author: adriaan westra
'** e-mail:
'**
'** purpose / comments:
'** determine if file is jpg image
'**
'** arguments :
'** strfilename : name of the file to check
'**
'** wijzigingslog :
'** 12/29/2008 11:02pm : initi雔e versie
'**
'*****************************************************************
function isjpg(strfilename)
set objregexp = new regexp
objregexp.pattern = "\w.jpg"
objregexp.ignorecase = true
isjpg = objregexp.test(strfilename)
end function
复制代码 代码如下:
'*****************************************************************
'** script: createxml.vbs
'** version: 1.0
'** created: 01/12/2009 9:51pm
'** author: adriaan westra
'** e-mail:
'** purpose / comments:
'** create xml file for photo album
'**
'**
'** changelog :
'** 12-01-2009 9:51 : initial version
'**
'*****************************************************************
on error resume next
dim version : version = "1.0" ' script version
dim author : author = "a. westra"
dim objxml 'xml document object
dim root 'root element of the xml document
dim newnode ' xml node object
dim cnode ' xml (child) node object
dim cnodetext ' xml text node object
'*****************************************************************
'** make sure the script is started with cscript
if instr(wscript.fullname, "wscript.exe") > 0 then
msgbox "please run this script with cscript.exe." & chr(13) & _
"for example : cscript " & wscript.scriptname & " /?", _
vbexclamation, wscript.scriptname
wscript.quit(1)
end if
'*****************************************************************
'** get commandline parameters
set args = wscript.arguments
if args.count = 0 then
strimagedir = inputbox("please give the directory name " & _
"to process : ",wscript.scriptname, strpath)
else
if instr(args(0),"/?") > 0 or instr(ucase(args(0)),"/h") > 0 _
or instr(ucase(args(0)),"/help") > 0 then
displayhelp
wscript.quit(0)
else
strimagedir = args(0)
end if
end if
set objxml = createobject("msxml2.domdocument.6.0")
objxml.setproperty "selectionlanguage", "xpath"
'*****************************************************************
'** determine if the file exists
strxmlfile = strimagedir & "\album.xml"
set objfso = createobject("scripting.filesystemobject")
if objfso.fileexists(strxmlfile) then
'*****************************************************************
'** read the xml file
objxml.load(strxmlfile)
else
'*****************************************************************
'** create the xml file
objxml.loadxml("")
end if
'*****************************************************************
'** process directory
set objimgdir = objfso.getfolder(strimagedir)
for each objfile in objimgdir.files
if isjpg(objfile.name) then
arrtemp = split(objfile.name, ".")
strnode = arrtemp(0)
'*****************************************************************
'** determine if the node exists
if not xmlnodeexists(strchildnode, objxml) then
'*****************************************************************
'** get the root element of the xml document
set root = objxml.documentelement
'*****************************************************************
'** create the new node
set newnode = objxml.createnode(1, strnode, "")
root.appendchild newnode
set cnode = objxml.createnode(1, "alt", "")
set cnodetext = objxml.createnode(3, "", "")
cnodetext.text = strnode
cnode.appendchild cnodetext
newnode.appendchild cnode
set cnode = objxml.createnode(1, "title", "")
set cnodetext = objxml.createnode(3, "", "")
cnodetext.text = strnode
cnode.appendchild cnodetext
newnode.appendchild cnode
end if
end if
next
'*****************************************************************
'** save the xml file
objxml.save(strxmlfile)
'*****************************************************************
'** end the script
wscript.quit
'*****************************************************************
'** function: xmlnodeexists
'** version: 1.0
'** created: 1/12/2009 12:14pm
'** author: adriaan westra
'** e-mail:
'**
'** purpose / comments:
'** determines if a node exists in xml
'**
'** arguments :
'** strnode :name of the xml node
'** oxml :xml dom object
'**
'** changelog :
'** 1/12/2009 12:16pm : initial version
'**
'*****************************************************************
function xmlnodeexists( strnode, oxml )
on error resume next
set onode = oxml.selectsinglenode(strnode)
strnodetype = onode.nodetype
if err.number = 0 then
xmlnodeexists = true
else
xmlnodeexists = false
end if
end function
'*****************************************************************
'** sub: displayhelp
'** version: 1.0
'** created: 24-03-2003 8:22
'** author: adriaan westra
'** e-mail:
'**
'** purpose / comments:
'** display help for script
'**
'** arguments :
'**
'** wijzigingslog :
'** 24-03-2003 8:22 : initi雔e versie
'**
'*****************************************************************
sub displayhelp()
strcomment = string(2,"*")
strcmntline = string(79, "*")
wscript.echo strcmntline
wscript.echo strcomment
wscript.echo strcomment & " online help for " & _
wscript.scriptname & " version : " & version
wscript.echo strcomment
wscript.echo strcomment & " usage : cscript " & _
wscript.scriptname & " directoryname"
wscript.echo strcomment
wscript.echo strcomment & " purpose : create xml file " & _
"for all images in given directory."
wscript.echo strcomment
wscript.echo strcomment & " author : " & author
wscript.echo strcomment & " e-mail : " & email
wscript.echo strcomment
wscript.echo strcmntline
end sub
'*****************************************************************
'** function: isjpg
'** version: 1.0
'** created: 12/29/2008 11:01pm
'** author: adriaan westra
'** e-mail:
'**
'** purpose / comments:
'** determine if file is jpg image
'**
'** arguments :
'** strfilename : name of the file to check
'**
'** wijzigingslog :
'** 12/29/2008 11:02pm : initi雔e versie
'**
'*****************************************************************
function isjpg(strfilename)
set objregexp = new regexp
objregexp.pattern = "\w.jpg"
objregexp.ignorecase = true
isjpg = objregexp.test(strfilename)
end function
上一篇: excel的导出和下载(实例讲解)