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

时间:2017-07-28 10:57:42

标签: vbscript hp-uft

我有一个包含XML内容的字符串,它基本上具有执行测试脚本所需的配置。

<br>var text="Hello ^^ World ^^ Break ^^ Line", txt=text.replace(/^^/g,"\n"); $SUB_SEND都是从配置文件中读取的。

$COUNTRY_CODE

现在我需要读取每一行并找到以$DOMAIN开头的单词的出现,并将其替换为配置文件。 如何在VBScript中获得以<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> 开头的所有单词?

1 个答案:

答案 0 :(得分: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>