使用Windows Batch比较3对XML文件

我试图创build一个batch file,读取存储在两个不同位置的3对相同的XML文件,并比较它们的构build版本是否相同或不使用xml文件中的构build标记和次要标记等不同的标记。

我有3个XML文件,master.xml,TGDD.xml和TMasterData.xml.I已经在2个不同的位置存储这3个XML文件,并希望检查他们的版本是否相同或不在这两个位置。

批码是..

@echo off setlocal enableextensions disabledelayedexpansion set "build=" set "build1=" set "gddbuild=" set "gddbuild1=" set "tbuild=" set "tbuild1=" set "ans=Master_Data version same" set "wrong=Master_Data version not same" set "ans1=TGDD version same" set "wrong1=TGDD version not same" set "ans2=TMasterData version same" set "wrong2=TMasterData version not same" for /f "tokens=3 delims=<>" %%a in (' 2^>nul type "C:\Users\Lucy\Desktop\piller-uniblock\master.xml" "C:\piller-uniblock\master.xml" ^| find /i "<Build>" ') do if not defined build ( set "build=%%a" ) else ( set "build1=%%a" ) IF "%build%"=="%build1%" ( echo %ans% ) else echo %wrong% for /f "tokens=3 delims=<>" %%b in (' 2^>nul type "C:\Users\Lucy\Desktop\piller-uniblock\TGDD.xml" "C:\piller-uniblock\TGDD.xml" ^| find /i "<Minor>" ') do if not defined gddbuild ( set "gddbuild=%%b" ) else ( set "gddbuild1=%%b" ) IF "%gddbuild%"=="%gddbuild1%" ( echo %ans1% ) else echo %wrong1% for /f "tokens=3 delims=<>" %%c in (' 2^>nul type "C:\Users\Lucy\Desktop\piller-uniblock\TMasterData.xml" "C:\piller-uniblock\TMasterData.xml" ^| find /i "<Build>" ') do if not defined tbuild ( set "tbuild=%%c" ) else ( set "tbuild1=%%c" ) IF "%tbuild%"=="%tbuild1%" ( echo %ans2% ) else echo %wrong2% 

输出..

产量

即使上面的代码在命令提示符下运行正常,我似乎无法理解如何使上面的代码更有效率,即不使用3循环。请帮助…

这3个XML文件的代码是…

master.xml

 <?xml version="1.0" encoding="UTF-8"?> <CDMDataXML xmlns="http://www.avocent.org/trellis/CDMLoaderXMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.avocent.org/trellis/CDMLoaderXMLSchema CDMLoaderXMLSchema.xsd"> <CDMDataVersion> <Major>1</Major> <Minor>0</Minor> <Build>50</Build> <Delimiter>.</Delimiter> </CDMDataVersion> 

TGDD.xml

  <?xml version="1.0" encoding="utf-8"?> <TrellisGdd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <TrellisVersion> <VersionId>1</VersionId> <Major>3</Major> <Delimiter>.</Delimiter> <Minor>32</Minor> <LastDictionaryEntry>3806</LastDictionaryEntry> </TrellisVersion> 

TMasterData.xml

 <?xml version="1.0" encoding="UTF-8"?> <CDMDataXML xmlns="http://www.avocent.org/trellis/CDMLoaderXMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.avocent.org/trellis/CDMLoaderXMLSchema CDMLoaderXMLSchema.xsd"> <CDMDataVersion> <Major>3</Major> <Minor>0</Minor> <Build>15</Build> <Delimiter>.</Delimiter> </CDMDataVersion> 

Solutions Collecting From Web of "使用Windows Batch比较3对XML文件"

缩短文件名称/路径以更好地查看呼叫,更改为完整文件路径。

 @echo off setlocal enableextensions disabledelayedexpansion call :compareXML "master.xml" "master1.xml" "Major, Minor, Build" if errorlevel 1 ( echo different ) else ( echo match ) call :compareXML "TGDD.xml" "TGDD1.xml" "VersionID, Major, Minor" if errorlevel 1 ( echo different ) else ( echo match ) call :compareXML "TMasterData.xml" "TMasterData1.xml" "Major, Minor, Build" if errorlevel 1 ( echo different ) else ( echo match ) exit /b :compareXML file1 file2 taglist setlocal enableextensions disabledelayedexpansion setlocal enableextensions enabledelayedexpansion set "match=" & for %%a in (%~3) do set "match=!match! /c:"^<%%a^>"" endlocal & set match=%match% for /f "tokens=1,2 delims=<> " %%a in (' type "%~1" "%~2" 2^> nul ^| findstr /i /l %match% ') do if not defined _F1_%%a ( set "_F1_%%a=%%b" ) else ( setlocal enabledelayedexpansion for %%c in ("!_F1_%%a!.") do ( endlocal if /i not "%%b."=="%%~c" ( endlocal & exit /b 1 ) set "_F1_%%a=" ) ) set _F1_ 2>nul && ( endlocal & exit /b 1 ) || ( endlocal & exit /b 0 ) 

由于需要对每对文件执行操作,因此代码已被移至子例程。 这个子程序被调用,每个文件的完整路径和要比较的标签列表。

这两个文件都是键入的,并且在找到标签时,为第一个文件设置环境变量,并将第二个文件的前一个值再次进行比较。 如果全部匹配,则文件具有相同的值,如果某些内容不匹配或者末尾的数字或标签不匹配(如果找到匹配,则删除变量,如果还有内容则不匹配)文件是不同的。