2017-08-07 129 views
0

我的操作系統是Windows   7 64位。我需要通過使用VBScript編寫以下XML字符串解釋動態內容的XML文件(也保持標籤縮進)沿:使用VBScript將XML字符串直接寫入XML文件

文件名:[Variable1]_[Variable2].xml

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

所有我可以嘗試的是在腳本下方逐個創建各種標籤和元素。

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" 

但是,這似乎是在未來可能的大XML文件太麻煩了,所以我可以只寫上面的XML字符串(與它們相關的值解釋所有變量)我提到的,直接到XML文件,然後給VBScript一個基於變量的動態名稱?

回答

1

通常,XML數據應該使用XML工具(DOM操作,XSLT)進行處理,這完全是因爲當問題的大小/複雜度增加時,這些方法往往會更好地擴展。

但是對於使用RegExp替換功能和字典的特殊情況(例如ASCII編碼,替換品的簡單標記)可以有效地解決模板化任務(請參閱here)。

演示代碼:

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")) 

輸出:

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> 
0

看看這有助於

Set xmlDoc = CreateObject("Msxml2.DOMDocument.3.0") 

xmlDoc.LoadXML strXML 'strXML being your xml string 

xmlDoc.Save strPath 'Directory and name of the save location 

對於這種方法工作,strXML變量應該有事先解釋變量。您可以使用字符串方法來實現這一點。

P.S.我已經在手機上寫了這個片段,可能會有一些錯誤,但這應該有前途。

+0

你可以在你的答案中添加一些字符串方法的演示,我真的需要在文檔中加載xml之前解析這些變量。 – VST