vbscript - Find and replace words in XML string -
i have string xml content, has configurations required execute test script.
$sub_send
, $country_code
, $domain
read config file.
<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>
now need read each line , find occurrence of words beginning $
, replace config file. how can words beginning $
in vbscript?
if can treat .xml plain ascii text, use regexp replace function , dictionary data:
option explicit ' path src file const p = "e:\work\proj\soa\tmp\45371693.xml" dim s : s = createobject("scripting.filesystemobject").opentextfile(p).readall() ' regexp find $ + seq of alphas or _ dim r : set r = new regexp r.global = true r.pattern = "\$[a-z_]+" ' find/replace pairs in 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 d($x) function f(m, p, s) if d.exists(m) m = d(m) f = m end function
output:
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>
Comments
Post a Comment