使用Windows Batch比较3对XML文件

时间:2014-09-18 11:19:35

标签: xml windows batch-file cmd

我正在尝试创建一个批处理文件,该文件读取存储在两个不同位置的3对相同的XML文件,并使用不同的标记(如build tag&)来比较它们的构建版本是否相同。 xml文件中的次要标记。

我有3个XML文件,master.xml,TGDD.xml和TMasterData.xml。我已将这3个xml文件存储在2个不同的位置,并想检查这两个位置的版本是否相同。

批次代码为..

@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%

输出..

output

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

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>

1 个答案:

答案 0 :(得分:1)

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

@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 )

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

两个文件都是键入的,并且在找到标记时,为第一个文件设置环境变量,并再次比较第二个文件的先前值。如果全部匹配,则文件具有相同的值,如果某些内容不匹配,或者最后数字或标签不匹配(变量在找到匹配项时被删除,如果某些内容仍然不匹配)文件不同。< / p>