使用VBScript将XML字符串直接写入XML文件 [英] Write XML string directly to XML file with VBScript

查看:187
本文介绍了使用VBScript将XML字符串直接写入XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的操作系统是Windows 7 64位.我需要使用VBScript将以下XML字符串以及解释后的动态内容写入XML文件(还保持选项卡缩进):

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:

文件名:[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"

但是对于将来可能很大的XML文件来说,这似乎太麻烦了,所以我可以直接将上面提到的XML字符串(所有变量都用其相关值解释)写到XML文件中,然后提供一个变量- VBScript基于动态名称?

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?

推荐答案

通常,应使用XML工具(DOM操作,XSLT)来处理XML数据,这恰恰是因为当这些方法的大小/复杂度较大时,这些方法往往会更好地扩展问题越来越严重.

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.

但是对于特殊情况(例如ASCII编码,万无一失的替换标记),使用RegExp替换功能和字典可以有效地解决模板任务(请参见

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

演示代码:

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>

这篇关于使用VBScript将XML字符串直接写入XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆