book.vbs
Option Explicit
if WScript.Arguments.length=0 then
WScript.echo "请指向数据源文件!"
WScript.quit
end if
Dim dbfile,db,sdb,i
Dim fso
Dim fo,nf
dbfile = WScript.Arguments(0)
WScript.echo("正在解析"+dbfile+"....")
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
if fso.FileExists(dbfile) Then
Set fo = fso.OpenTextFile(dbfile,1)
db = split(fo.ReadAll,Chr(13)&Chr(10))
For each sdb in db
If UBound(split(sdb," |"))<>6 Then
WScript.echo vbCrlf & "未知的数据源文件:" & sdb
WScript.echo "正确的格式:总排序号 |ISBN |书名 |作者/翻译 |出版社 |价格 |图片" & vbCrlf
Else
autoXML split(sdb," |")
End If
Next
Else
WScript.echo "请指向数据源文件!"
WScript.quit
End If
function autoXML(ary)
On Error Resume Next
Dim xmlFile
Dim xmldoc,root,books
xmlFile = "book.xml"
Set xmldoc = WScript.CreateObject("Microsoft.XMLDOM")
xmldoc.async = false
xmldoc.load xmlFile
if xmldoc.parseError.errorCode Then
Dim xmlhead,xslhead
Set xmlhead = xmldoc.createProcessingInstruction("xml","version=""1.0"" encoding=""UTF-8""")
xmldoc.insertBefore xmlhead,xmldoc.childNodes(0)
Set xslhead = xmldoc.createProcessingInstruction("xml-stylesheet","type=""text/xsl"" href=""book.xsl""")
xmldoc.insertBefore xslhead,xmldoc.childNodes(1)
Set books = xmldoc.createElement("books")
xmldoc.appendChild books
Else
Set books = xmldoc.documentElement
End If
Dim book,nodes,node
Set book = xmldoc.createElement("book")
books.appendChild book
nodes = split("id,ISBN,book_name,writer,publish,price,img",",")
for i=0 to UBound(nodes)
Set node = xmldoc.createElement(nodes(i))
node.text = ary(i)
book.appendChild node
Next
xmldoc.save xmlFile
If Err.number<>0 Then
WScript.echo "保存失败!原因:" & Err.Description
Else
WScript.echo "《" & ary(2) & "》保存成功!"
End If
End Function