发现和在XML文件中更换多个串 [英] Finding and Replacing multiple strings in an XML file

查看:130
本文介绍了发现和在XML文件中更换多个串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到和单个或多个XML替换多个字符串文件的一次。我曾尝试参数化XML输入文件和传递变量查找和替换匹配模式。使用循环费时,因为它是一个嵌套的循环。我们可以编写使用是否存在

在code如下:结果
- 对查找/替换脚本

 关闭@echo
SETLOCAL ENABLEEXTENSIONS disabledelayedexpansion
设置搜索=%〜1
将替换=%〜2,
设置文本文件=%3  FOR / Fdelims =%% i的('键入%TEXTFILE%^&安培;打破^>中TEXTFILE%%')做(
        一套行= %%我
        SETLOCAL enabledelayedexpansion
        一套行=行:%搜索%=%替换%!
        >>!%TEXTFILE%回声(线!
        ENDLOCAL
    )

- 输入变量

 关闭@echo设置源=<&中用srcPath GT;
设定的目标= LT; TGTpath>XCOPY / Y%LT; SRC>%\\ *%LT; TGT>%\\ *。
DIR / S / B%TGT%\\ *。xml的。> filenames.txt - 多I / P文件
设置文件名=< I / P> \\ filenames.txt
设置paramfile =<变量值路径> \\ parm.txt
FOR / F令牌= *%%我(类型文件名%%)DO(
FOR / F令牌= 1,2 delims =%% j中(类型%PARM%)并调用<查找/替换script.bat> %%Ĵ%%ķ%%我


解决方案

对不起。如果我理解正确的话你的注释的,你的问题的的查找和在一个XML文件替换多个字符串(因为你已经有了一个code是解决这一问题),但这样做的以有效的方式的!你应该在你的要求非常明确,否则你将得到不解决您的问题(即浪费尽力帮助你的人的时间)的答案。

您code有几个构造,特别慢。下面的code使用的相同的方法来查找和替换字符串,但使用更高效的结构。

 关闭@echo设置源=<&中用srcPath GT;
设定的目标= LT; TGTpath>XCOPY / Y%LT; SRC>%\\ *%LT; TGT>%\\ *。
设置paramfile =<变量值路径> \\ parm.txt
REM处理所有.xml文件
FOR / F令牌= *%%的F('DIR / S / B%TGT%\\ *。xml的')做(
   REM过程中的每个文件中的所有行
   (FOR / F在有usebackq delims =%%我(%% F)做(
      一套行= %%我
      SETLOCAL EnableDelayedExpansion
      REM替换paramfile的所有字符串
      FOR / F令牌有usebackq = 1,2在(%PARM%)%% j执行(
         一套行=行:%% J = %%ķ!
      )
      回声(!行!
      ENDLOCAL
   ))GT; tempfile.tmp
   移动/ Y tempfile.tmp%% F

请注意,我用的您完全相同的code 的,而我只是重新安排它以提高性能。这意味着,你必须修复任何缺失值,错误等你code可能有。

修改为增加替换行范围

下面的修改允许指定行执行查找/替换的特定范围;可选范围在批处理文件中的参数说明。

 关闭@echo
SETLOCAL设置ifInRange =
设置ENDIF =
REM如果两个参数给出,它们指定为替代线的期望的范围
如果%〜2EQU,转到开始设置ifInRange =如果%% ^ h GEQ%1 %%如果ħ当量%2(
一套是endif =):开始设置源=<&中用srcPath GT;
设定的目标= LT; TGTpath>XCOPY / Y%LT; SRC>%\\ *%LT; TGT>%\\ *。
设置paramfile =<变量值路径> \\ parm.txt
REM处理所有.xml文件
FOR / F令牌= *%%的F('DIR / S / B%TGT%\\ *。xml的')做(
   REM过程中的每个文件中的所有行
   (FOR / F令牌= 1 * delims =%%的('FINDSTR / N^%% F)H做(
      一套行= %%我
      SETLOCAL EnableDelayedExpansion
      如果一个行范围被赋予物权检查
      %ifInRange%
         REM替换paramfile的所有字符串
         FOR / F令牌有usebackq = 1,2在(%PARM%)%% j执行(
            一套行=行:%% J = %%ķ!
         )
      %万一%
      回声(!行!
      ENDLOCAL
   ))GT; tempfile.tmp
   移动/ Y tempfile.tmp%% F

编辑#2 回复评论

您可以指定行作为批处理文件中的参数范围。例如,以处理从20行至60:

  batchFileName.bat 20 60

但是,如果行的范围始终是一样的,你可以简化code是这样的:

 关闭@echo
SETLOCAL设置源=<&中用srcPath GT;
设定的目标= LT; TGTpath>XCOPY / Y%LT; SRC>%\\ *%LT; TGT>%\\ *。
设置paramfile =<变量值路径> \\ parm.txt
REM处理所有.xml文件
FOR / F令牌= *%%的F('DIR / S / B%TGT%\\ *。xml的')做(
   REM过程中的每个文件中的所有行
   (FOR / F令牌= 1 * delims =%%的('FINDSTR / N^%% F)H做(
      一套行= %%我
      SETLOCAL EnableDelayedExpansion
      REM检查中,该行范围
      如果%% ^ h GEQ 20 %%,如果ħ60当量(
         REM替换paramfile的所有字符串
         FOR / F令牌有usebackq = 1,2在(%PARM%)%% j执行(
            一套行=行:%% J = %%ķ!
         )
      )
      回声(!行!
      ENDLOCAL
   ))GT; tempfile.tmp
   移动/ Y tempfile.tmp%% F

I want to find and replace multiple strings in single or multiple XML files all at once. I have tried parameterizing the xml input files and passing variables for finding and replacing matching patterns. Using a for loop is time-consuming, as it is a nested for loop. can we write using "if exists"

The code is as follows:
-- for find/replace script

@echo off
setlocal enableextensions disabledelayedexpansion
set "search="%~1""
set "replace="%~2""
set "textfile=%3"

  for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
        set "line=%%i"
        setlocal enabledelayedexpansion
        set "line=!line:%search%=%replace%!"
        >>"%textFile%" echo(!line!
        endlocal
    )

-- input variables

 @echo off

set source=<SRCpath>"
set target=<TGTpath>

xcopy /Y %<SRC>%\* %<TGT>%\*
dir /s /b "%TGT%\*.xml" > filenames.txt -- for multi i/p files
set filename="<i/p>\filenames.txt"
set paramfile="<variable values path>\parm.txt"
FOR /F "tokens=*" %%i IN ('type "%filenames%"') DO (
for /f "tokens=1,2 delims=" %%j in ('type "%parm%"') do call <Find/Replace script.bat> %%j %%k %%i 
)

解决方案

Excuse me. If I correctly understood your comments, your problem is not "Finding and Replacing multiple strings in an XML file" (because you already have a code that solve that problem), but do that in an efficient way! You should be very clear in your requests, otherwise you will get answers that don't solve your problem (wasting the time of the people that try to help you).

Your code have several constructs that are particularly slow. The code below uses your same method to "finding and replacing strings", but using more efficient constructs.

@echo off

set source=<SRCpath>"
set target=<TGTpath>

xcopy /Y %<SRC>%\* %<TGT>%\*
set paramfile="<variable values path>\parm.txt"
rem Process all .xml files
FOR /F "tokens=*" %%f in ('dir /s /b "%TGT%\*.xml"') do (
   rem Process all lines in each file
   (for /f "usebackq delims=" %%i in ("%%f") do (
      set "line=%%i"
      setlocal EnableDelayedExpansion
      rem Replace all strings in paramfile
      for /f "usebackq tokens=1,2" %%j in ("%parm%") do (
         set "line=!line:%%j=%%k!"
      )
      echo(!line!
      endlocal
   )) > tempfile.tmp
   move /Y tempfile.tmp "%%f"
)

Note that I used your exact same code and that I just rearranged it in order to improve the performance. This means that you must fix any missing value, error, etc. that your code may have.

EDIT: Range of lines for replacements added

The modification below allows to specify a particular range of lines to perform find/replace; the optional range is given in the parameters of the Batch file.

@echo off
setlocal

set "ifInRange="
set "endIf="
rem If two parameters are given, they specify the desired range of lines for replacements
if "%~2" equ "" goto begin

set "ifInRange=if %%h geq %1 if %%h leq %2 ("
set "endIf=)"

:begin

set source=<SRCpath>"
set target=<TGTpath>

xcopy /Y %<SRC>%\* %<TGT>%\*
set paramfile="<variable values path>\parm.txt"
rem Process all .xml files
FOR /F "tokens=*" %%f in ('dir /s /b "%TGT%\*.xml"') do (
   rem Process all lines in each file
   (for /f "tokens=1* delims=:" %%h in ('findstr /N "^" "%%f"') do (
      set "line=%%i"
      setlocal EnableDelayedExpansion
      rem Check if a line range was given
      %ifInRange%
         rem Replace all strings in paramfile
         for /f "usebackq tokens=1,2" %%j in ("%parm%") do (
            set "line=!line:%%j=%%k!"
         )
      %endIf%
      echo(!line!
      endlocal
   )) > tempfile.tmp
   move /Y tempfile.tmp "%%f"
)

EDIT #2: Reply to comment

You may specify the range of lines as parameters of the Batch file. For example, to process lines from 20 to 60:

batchFileName.bat 20 60

However, if the range of lines is always the same, you may simplify the code this way:

@echo off
setlocal

set source=<SRCpath>"
set target=<TGTpath>

xcopy /Y %<SRC>%\* %<TGT>%\*
set paramfile="<variable values path>\parm.txt"
rem Process all .xml files
FOR /F "tokens=*" %%f in ('dir /s /b "%TGT%\*.xml"') do (
   rem Process all lines in each file
   (for /f "tokens=1* delims=:" %%h in ('findstr /N "^" "%%f"') do (
      set "line=%%i"
      setlocal EnableDelayedExpansion
      rem Check if the line is in range
      if %%h geq 20 if %%h leq 60 (
         rem Replace all strings in paramfile
         for /f "usebackq tokens=1,2" %%j in ("%parm%") do (
            set "line=!line:%%j=%%k!"
         )
      )
      echo(!line!
      endlocal
   )) > tempfile.tmp
   move /Y tempfile.tmp "%%f"
)

这篇关于发现和在XML文件中更换多个串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆