查找并替换XML字符串中的单词

问题描述:

我有一个XML内容的字符串,它基本上具有执行测试脚本所需的配置。查找并替换XML字符串中的单词

$SUB_SEND$COUNTRY_CODE,$DOMAIN都是从配置文件中读取的。

<Provisioning xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Request><Header><Command>Create</Command><EntityIdentifiers><Identifier Type="TelephoneNumber" Value="$SUB_SEND"/></EntityIdentifiers><EntityName>Subscriber</EntityName></Header><Data><Subscriber><!--<RcvMaxMmsMsgSize>53</RcvMaxMmsMsgSize>--><OperatorCode>54</OperatorCode><SendAutoReply>0</SendAutoReply><CopyReceivedMessagesEnabled>0</CopyReceivedMessagesEnabled><RequestMmsDeliveryReport>1</RequestMmsDeliveryReport><CopySentMessagesEnabled>0</CopySentMessagesEnabled><SendMmsToMbx>0</SendMmsToMbx><AddSignature>0</AddSignature><SubscriberCosName>Standard MMS</SubscriberCosName><!--<SendMmsMaxAttachNum>50</SendMmsMaxAttachNum>--><!--<SendMmsMaxRcptNum>51</SendMmsMaxRcptNum>--><!--<HandsetType>LegacyPhone</HandsetType>--><SubscriberDomainName>$DOMAIN</SubscriberDomainName><AutoProvIndication>1</AutoProvIndication><!--<SendMaxMmsMsgSize>52</SendMaxMmsMsgSize>--><MmsUserType>None</MmsUserType><BWListInUse>None</BWListInUse><AllowMmsDeliveryReport>1</AllowMmsDeliveryReport><!--<BillingType>PrePaid</BillingType>--><CountryCode>**$COUNTRY_CODE**</CountryCode><SubscriberName>$SUB_SEND</SubscriberName></Subscriber></Data></Request></Provisioning> 

现在我需要读取每一行,并找到$开头的单词的出现和使用的配置文件将其替换。 如何在VBScript中获得以$开头的所有单词?

+0

如果你在一个字符串有这只是使用'myXmlString = Replace(myXmlString,“$ SUB_SEND”,“MyValueFromConfig”)'并重复'$ COUNTRY_CODE'和'$ DOMAIN' – Dave

+0

让我添加更多点。我不想硬编码它,我想读取以$开头的所有单词并将其保存在字符串中,并将其替换为配置值。因此,该解决方案将更通用,如果有人添加更多配置元素,并且不需要添加更多行来替换。 – Sameer

+0

你到目前为止尝试过什么? SO不是免费的代码写入服务。另外,你想替代的价值来自哪里? –

如果你真的可以把你的.xml为纯ASCII文本,使用regexp replace function和词典数据:

Option Explicit 

' path to src file 
Const p = "e:\work\proj\soa\tmp\45371693.xml" 

Dim s : s = CreateObject("Scripting.FileSystemObject").OpenTextFile(p).ReadAll() 

' Regexp to find $ + Seq of Alphas or _ 
Dim r : Set r = New RegExp 
r.Global = True 
r.Pattern = "\$[A-Z_]+" 

' Find/Replace pairs in a dictionary 
Dim d : Set d = CreateObject("Scripting.Dictionary") 
d("$SUB_SEND") = "Abra" 
d("$DOMAIN") = "cada" 
d("$COUNTRY_CODE") = "bra" 

' RegExp replace using function f 
s = r.Replace(s, GetRef("f")) 

WScript.Echo s 

WScript.Quit 0 

' replace found $X with d($X) 
Function f(m, p, s) 
    If d.Exists(m) Then m = d(m) 
    f = m 
End Function 

输出:

cscript 45371693-2.vbs 
<Provisioning xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <Request> 
       <Header> 
         <Command>Create</Command> 
         <EntityIdentifiers> 
           <Identifier Type="TelephoneNumber" Value="Abra"/> 
         </EntityIdentifiers> 
         <EntityName>Subscriber</EntityName> 
       </Header> 
       <Data> 
         <Subscriber> 
           <!--<RcvMaxMmsMsgSize>53</RcvMaxMmsMsgSize>--> 
           <OperatorCode>54</OperatorCode> 
           <SendAutoReply>0</SendAutoReply> 
           <CopyReceivedMessagesEnabled>0</CopyReceivedMessagesEnabled> 
           <RequestMmsDeliveryReport>1</RequestMmsDeliveryReport> 
           <CopySentMessagesEnabled>0</CopySentMessagesEnabled> 
           <SendMmsToMbx>0</SendMmsToMbx> 
           <AddSignature>0</AddSignature> 
           <SubscriberCosName>Standard MMS</SubscriberCosName> 
           <!--<SendMmsMaxAttachNum>50</SendMmsMaxAttachNum>--> 
           <!--<SendMmsMaxRcptNum>51</SendMmsMaxRcptNum>--> 
           <!--<HandsetType>LegacyPhone</HandsetType>--> 
           <SubscriberDomainName>cada</SubscriberDomainName> 
           <AutoProvIndication>1</AutoProvIndication> 
           <!--<SendMaxMmsMsgSize>52</SendMaxMmsMsgSize>--> 
           <MmsUserType>None</MmsUserType> 
           <BWListInUse>None</BWListInUse> 
           <AllowMmsDeliveryReport>1</AllowMmsDeliveryReport> 
           <!--<BillingType>PrePaid</BillingType>--> 
           <CountryCode>**bra**</CountryCode> 
           <SubscriberName>Abra</SubscriberName> 
         </Subscriber> 
       </Data> 
     </Request> 
</Provisioning>