用于Xml修改的批处理文件 [英] Batch File for Xml modification
问题描述
我有一个xml文件,必须根据一些规则对它进行修改,我想知道我们可以在批处理脚本中进行此操作.假设以下是xml文件的示例
i have an xml file on which i have to do modification basing on some rules and i want to know can we do that in the batch script. suppose the following is the sample of xml file
> > <conf-article article-type="research" content-type="----"
> > dtd-version="---" open-access="no" xml:lang="en" lifecycle="final">
> > <conf-front>
<conf-proc-meta>
<conf-proc-id
> > conf-proc-id-type="conf-acronym">cccc</conf-proc-id>
> > <conf-proc-title-group>
我想插入以下几行
<!--Delivery Date: 07/23/2013-->
<!--XML Script: sdfasdfdfs-->
<!--Batch: sdfsdfdssfs-->
在<conf-front>
标记之前.这是一个示例,例如我还有更多.因此,我需要一些帮助.
before <conf-front>
tag.This is an example like that i have some more.so i need some help on this.
推荐答案
@echo off
::get the line before desired tag
for /f "tokens=1 delims=:" %%L in ('findstr /n "<conf-front>" some.xml') do set /a line=%%L-1
::create empty file
break>"%temp%\empty"
::get first <%line%> lines of the file
fc "%temp%\empty" "some.xml" /lb %line% /t |more +4 | findstr /B /E /V "*****" >temp.xml
:: add additional content
echo ^<!--Delivery Date: 07/23/2013--^> >> temp.xml
echo ^<!--XML Script: sdfasdfdfs--^> >> temp.xml
echo ^<!--Batch: sdfsdfdssfs--^> >> temp.xml
::get last <%line%> lines of the file
type "some.xml" | more +%line% >> temp.xml
::delete temp empty file
del /Q /F "%temp%\empty"
这将创建temp.xml文件,您可以检查它是否适合您的需求,然后将其复制到旧文件中.请将其设置在 last <conf-front>
标记之前,所以我希望只有其中一种.然后在代码中到处更改some.xml
和xml名称.
This will create temp.xml file and you can check if it fits on your needs and then copy it over your old file.Will set this before last <conf-front>
tag so I hope there's only one of this kind.And change some.xml
with the name of your xml everywhere in the code.
编辑〜使用文件夹中的所有.xml进行此操作(不会影响子文件夹)
EDIT ~ do this with all .xml in a folder (will not affect subfolders)
@echo off
rem cd /d "c:\some_dir"
::create empty file
break>"%temp%\empty"
:: iterate trough the xml files
setlocal enableDelayedExpansion
for %%X in (*.xml) do (
set "line="
::get the line before desired tag
for /f "tokens=1 delims=:" %%L in ('findstr /n "<conf-front>" %%~dpsnxX') do set /a line=%%L-1
::only in case the tag is presented
if defined line (
::get first <%line%> lines of the file
fc "%temp%\empty" "%%~dpsnxX" /lb !line! /t |more +4 | findstr /B /E /V "*****" >%temp%\temp.%%~nX.xml
:: add additional content
echo ^<^^!--Delivery Date: 07/23/2013--^> >>"%temp%\temp.%%~nX.xml"
echo ^<^^!--XML Script: sdfasdfdfs--^> >>"%temp%\temp.%%~nX.xml"
echo ^<^^!--Batch: sdfsdfdssfs--^> >>"%temp%\temp.%%~nX.xml"
::get last <%line%> lines of the file
type "%%~dpsnxX" | more +!line! >>"%temp%\temp.%%~nX.xml"
)
)
endlocal
:: replace the file with these in temp folder with new content
for %%X in (*.xml) do (
move /Y "%temp%\temp.%%~nX.xml" "%%~dpnxX" 2>nul
)
::delete temp empty file
del /Q /F "%temp%\empty"
只需用自己的路径更改c:\some_dir
.然后备份目录.
Just change c:\some_dir
with your own path.And back-up your dir..
这篇关于用于Xml修改的批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!