2017-07-28 77 views
1

我有一個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不是免費的代碼寫入服務。另外,你想替代的價值來自哪裏? –

回答

1

如果你真的可以把你的.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>