比较使用Windows批量3双XML文件 [英] Compare 3 pairs of XML files using Windows Batch

查看:104
本文介绍了比较使用Windows批量3双XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个批处理文件,其内容3对存放在两个不同的地点相同的XML文件,并比较其构建版本是否相同或者不使用来样订做标签和放不同的标签;轻微的标记在XML文件中。

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

批code是..

 关闭@echo
SETLOCAL ENABLEEXTENSIONS disabledelayedexpansion设置建=
设置build1中=设置gddbuild =
设置gddbuild1 =设置tbuild =
设置tbuild1 =
设置ANS = Master_Data版一样的
设置错= Master_Data版本不一样设置ANS1 = TGDD版一样的
设置wrong1 = TGDD版本不一样设置ANS2 = TMasterData版一样的
设置wrong2 = TMasterData版本不一样
FOR / F令牌= 3 delims =<>中%%一个在('
    2 ^> NUL键入C:\\用户\\露\\桌面\\皮勒-uniblock \\ master.xml
                C:\\皮勒-uniblock \\ master.xml
    ^ |发现/我<构建与GT;
')不要如果没有定义的构建(集构建= %% A)否则(设置build1中= %% A)IF%积聚%==%build1中%(回声%答%),其他的回声%错误%FOR / F令牌= 3 delims =<>中在%% B('
    2 ^> NUL键入C:\\用户\\露\\桌面\\皮勒-uniblock \\ TGDD.xml
                C:\\皮勒-uniblock \\ TGDD.xml
    ^ |发现/我<小>中
')做的。如果没有定义gddbuild(SETgddbuild = %% B),其他(SETgddbuild1 = %% B)IF%gddbuild%==%gddbuild1%(回声%ANS1%),其他的回声%wrong1%
FOR / F令牌= 3 delims =<>中%%℃的('
    2 ^> NUL键入C:\\用户\\露\\桌面\\皮勒-uniblock \\ TMasterData.xml
                C:\\皮勒-uniblock \\ TMasterData.xml
    ^ |发现/我<构建与GT;
')不要。如果没有定义tbuild(集tbuild = %% C),其他(SETtbuild1 = %% C)IF%tbuild%==%tbuild1%(回声%ANS2%),其他的回声%wrong2%

输出..

即使上面code在命令提示符下运行正常,我似乎无法了解如何使上述code的effcient即不使用3 loops.Please帮助...

在code为3 XML文件是...

master.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< CDMDataXML的xmlns =htt​​p://www.avocent.org/trellis/CDMLoaderXMLSchema的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instanceXSI:=的schemaLocationHTTP:// www.avocent.org/trellis/CDMLoaderXMLSchema CDMLoaderXMLSchema.xsd>
< CDMDataVersion>
<主要大于1< /主要与GT;
<小> 0℃; /小>
<构建与GT; 50℃/构建>
<分隔符方式>< /分隔符>
< / CDMDataVersion>

TGDD.xml

 <?XML版本=1.0编码=UTF-8&GT?;
 < TrellisGdd的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =htt​​p://www.w3.org/2001/XMLSchema>
 < TrellisVersion>
 &所述; VERSIONID→1&下; / VERSIONID>
 <主要>第3版; /主要与GT;
 <分隔符方式>< /分隔符>
 <小> 32 LT; /小>
 < LastDictionaryEntry> 3806< / LastDictionaryEntry>
 < / TrellisVersion>

TMasterData.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< CDMDataXML的xmlns =htt​​p://www.avocent.org/trellis/CDMLoaderXMLSchema的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instanceXSI:=的schemaLocationHTTP:// www.avocent.org/trellis/CDMLoaderXMLSchema CDMLoaderXMLSchema.xsd>
 < CDMDataVersion>
    <主要>第3版; /主要与GT;
    <小> 0℃; /小>
    <构建与GT; 15℃; /构建>
    <分隔符方式>< /分隔符>
 < / CDMDataVersion>


解决方案

文件名/路径缩短,以便更好地查看通话,切​​换到完整的文件路径。

 关闭@echo
    SETLOCAL ENABLEEXTENSIONS disabledelayedexpansion    拨打:compareXMLmaster.xmlmaster1.xml,主要,次要,构建
    如果错误级别1(回声不同)其他(回声匹配)    拨打:compareXMLTGDD.xmlTGDD1.xmlVERSIONID,主要,次要
    如果错误级别1(回声不同)其他(回声匹配)    拨打:compareXMLTMasterData.xmlTMasterData1.xml,主要,次要,构建
    如果错误级别1(回声不同)其他(回声匹配)    退出/ B:compareXML文件1文件2标记列表
    SETLOCAL ENABLEEXTENSIONS disabledelayedexpansion    SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion
    集匹配=&放大器;对于一个%%在(%〜3)不要将匹配=匹配/ C:!^< %%一个^>中
    ENDLOCAL&安培;集匹配=%匹配%    FOR / F令牌= 1,2 delims =<>中%%一个在('
        键入%〜1%〜22 ^> NUL ^ | FINDSTR / I / 1%匹配%
    ')怎么做,如果没有定义_F1 _ %%一(一套_F1 _ %% A = %% B),其他(
        SETLOCAL enabledelayedexpansion
        在%% C(!_F1 _ %%一个!)做(
            ENDLOCAL
            如果/没有我。%% B==%%〜C(ENDLOCAL&安培;出口/ B 1)
            集_F1 _ %%一个=
        )
    )
    设置_F1_ 2 - ; NUL&放大器;&放大器; (ENDLOCAL&安培;出口/ B 1)|| (ENDLOCAL&安培;出口/ B 0)

由于操作需要对每对文件的将要执行时,code已被移动到一个子程序。该子程序被调用的每个文件和完整路径的标签的列表进行比较。

这两个文件类型的,并作为标记发现,环境变量为所述第一文件集和agains用于第二文件previous值进行比较。如果所有的比赛,该文件具有相同的价值观,如果事情不匹配或结尾的号码或标签不匹配(变量去除火柴发现,如果事情仍是一个不匹配)的文件是不同的。

I'm trying to create a batch file which reads 3 pair of identical XML files stored in two different locations and compares whether their build versions are same or not using different tags like build tag & minor tag in the xml files.

I have 3 XML files, master.xml,TGDD.xml and TMasterData.xml.I have stored these 3 xml files in 2 different locations and want to check whether their versions are same or not in these two locations.

Batch code is..

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

The output..

Even though the above code is running properly in the command prompt, I can't seem to understand how to make the above code more effcient i.e without using 3 for loops.Please Help...

The code for the 3 XML files are...

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>

解决方案

File names/paths shortened to better see the call, change to full file paths.

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

As the operation needs to be performed for each pair of files, the code has been moved to a subroutine. This subroutine is called with the full path for each file and the list of the tags to be compared.

Both files are typed, and as the tags are found, environment variables are set for the first file and compared agains the previous value for the second file. If all match, the files have the same values, if something does not match or at the end the number or tags do not match (variables are removed as matches are found, if something remains it is a no match) files are different.

这篇关于比较使用Windows批量3双XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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