How to modify a word in XML which has similiar set

2019-06-03 00:18发布

问题:

Following is an Example XML, I need to replace D74DVP1 in all the places. Please help me ? I could only able to replace the word only in first Node.

<?xml version="1.0"?>
    <Forest>
        <Domain>
            <!-- PartitionType:Application -->
            <Guid>01706323-d9a7-4f8d-90d0-1bfb7c88d1d0</Guid>
            <DNSname>ForestDnsZones.D74DVP1.local</DNSname>
            <NetBiosName></NetBiosName>
            <DcName></DcName>
        </Domain>
        <Domain>
            <!-- PartitionType:Application -->
            <Guid>5aca253d-4f3f-475b-b76e-13cd3c694570</Guid>
            <DNSname>ForestDnsZones.D74DVP1.local</DNSname>
            <NetBiosName></NetBiosName>
            <DcName></DcName>
        </Domain>
        <Domain>
            <!-- ForestRoot -->
            <Guid>87d8593d-fcee-4e75-acb6-67b415f5d4a8</Guid>
            <DNSname>D74DVP1.local</DNSname>
            <NetBiosName>D74DVP1</NetBiosName>
            <DcName></DcName>
        </Domain>
    </Forest>

回答1:

with replacer.bat

call replacer.bat "C:\Example.xml" "D74DVP1" "SomethingElse"

?



回答2:

You may use a Batch file solution, but it is problematic and slow. You may use a Batch-JScript hybrid script like JREPL.BAT or FindRepl.bat instead, that run much faster and have the benefits of regular expressions, but it seems too much just to achieve a simple replace in one file. Fortunately, you may also use the same approach of this solution: write a small Batch-JScript hybrid script specifically developed to solve just this problem. Here it is:

@set @a=0  /*
@cscript //nologo //E:JScript "%~F0" < Example.xml > Example.new
@goto :EOF */

WScript.Stdout.Write(WScript.StdIn.ReadAll().replace(/D74DVP1/g,"SomethingElse"));

For further details on what those strange @lines means, see this post. For a detailed explanation of JScript regular expressions, see here.

PS - Perhaps you forgot the "g" (general) option in the regexp of your VBScript (unposted) code?