0

我的操作系统是 Windows 7 64 位。我需要使用 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?

4

2 回答 2

1

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>
于 2017-08-07T20:13:54.890 回答
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 变量应该预先解释变量。您可以使用字符串方法来实现这一点。

PS我已经在手机上写了这个片段,可能有一些错误,但这应该让我们前进。

于 2017-08-07T21:51:14.267 回答