Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

windows - Write XML string directly to XML file with VBScript

My OS is Windows 7 64Bit. I need to write the following XML string along with interpreted dynamic content to an XML file (also maintaining the tabs indentation) by using VBScript:

File Name: [Variable1]_[Variable2].xml

<?xml version="1.0"?>
<config>
    <modules>
        <[Variable1]_[Variable2]>
            <active>true</active>
            <codePool>[Variable3]</codePool>
            <version>[Variable4]</version>
        </[Variable1]_[Variable2]>
    </modules>
</config>

All I could try for that is below script to create various tags and elements one-by-one.

scriptDir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
Set xmlDoc = CreateObject("Msxml2.DOMDocument.3.0")
Set objRoot = xmlDoc.CreateElement("config")
xmlDoc.AppendChild objRoot
Set objRecord = xmlDoc.CreateElement("modules")
objRoot.AppendChild objRecord
Set objName = xmlDoc.CreateElement("Mageweb_ShippingFilter")
objName.Text = ""
objRecord.AppendChild objName
Set objDate = xmlDoc.CreateElement("AuditDate")
objDate.Text = Date
objRecord.AppendChild objDate
Set objIntro = xmlDoc.CreateProcessingInstruction("xml", "version='1.0'")
xmlDoc.InsertBefore objIntro, xmlDoc.ChildNodes(0)
'For now there is static name of file
xmlDoc.Save scriptDir & "Testfile.xml"

But this seems to be too cumbersome for possibly large XML files in future, so can I just write the above XML string (with all variables interpreted with their relevant values) I mentioned, directly to XML file and then give a variable-based dynamic name with VBScript?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In general, XML data should be processed with XML tools (DOM manipulation, XSLT), exactly because those methods tend to scale better when the size/complexity of the problem grows.

But for special cases (e.g. ASCII encoding, foolproof marking of the replacements) using a RegExp replace function and a dictionary may solve a templating task efficiently (see here).

Demo code:

Option Explicit

Function fnRepl(sM, nP, sS)
  fnRepl = gdX(sM)
End Function

Function mkDic(aK, aV)
  Dim tmp : Set tmp = CreateObject("Scripting.Dictionary")
  Dim i
  For i = 0 To UBound(aK)
      tmp(aK(i)) = aV(i)
  Next
  Set mkDic = tmp
 End Function

Dim gdX : Set gdX = mkDic( _
     Split("[Variable1] [Variable2] [Variable3] [Variable4]") _
   , Split("abra cada bra sesame") _
) 
Dim r : Set r = New RegExp
r.Global = True
r.Pattern = "[[^]]+]" 
Dim sT : sT = Join(Array( _
      "<?xml version=""1.0""?>" _
    , "<config>" _
    , " <modules>" _
    , "  <[Variable1]_[Variable2]>" _
    , "   <active>true</active>" _
    , "   <codePool>[Variable3]</codePool>" _
    , "   <version>[Variable4]</version>" _
    , "  </[Variable1]_[Variable2]>" _
    , " </modules>" _
    , "</config>" _
    ), vbCrLf)

WScript.Echo sT
WScript.Echo "----------"
WScript.Echo r.Replace(sT, GetRef("fnRepl"))

output:

cscript 45553911.vbs
<?xml version="1.0"?>
<config>
 <modules>
  <[Variable1]_[Variable2]>
   <active>true</active>
   <codePool>[Variable3]</codePool>
   <version>[Variable4]</version>
  </[Variable1]_[Variable2]>
 </modules>
</config>
----------
<?xml version="1.0"?>
<config>
 <modules>
  <abra_cada>
   <active>true</active>
   <codePool>bra</codePool>
   <version>sesame</version>
  </abra_cada>
 </modules>
</config>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...