我试图解析一个XML文件,我需要能够将一个参数传递给一个批处理文件" DEV2_BUSINESSAPPS_URL"如果找到我想要" {{3 }}"回。不幸的是,它必须是一个批处理文件。
这是我的XML文件:
<Environment>
<Variable>
<Name>DEV1_BUSINESSAPPS_URL</Name>
<Value><![CDATA[https://energy.epipelines.com/businessapps/welcome.html]]></Value>
</Variable>
<Variable>
<Name>DEV2_BUSINESSAPPS_URL</Name>
<Value><![CDATA[https://energy.epipelines.com/businessapps/welcome.html]]></Value>
</Variable>
<Variable>
<Name>DEV3_BUSINESSAPPS_URL</Name>
<Value><![CDATA[https://energy.epipelines.com/businessapps/welcome.html]]></Value>
</Variable>
<Variable>
<Name>QA1_BUSINESSAPPS_URL</Name>
<Value><![CDATA[https://qa1comapps.elm.com/businessapps]]></Value>
</Variable>
</Environment>
答案 0 :(得分:0)
我将如何做到这一点。它非常脆弱,因为让批处理理解标记文件布局的差异并不容易,因为它可能会像XML一样变化。但是,只要文件采用您指定的格式,这应该可行。
@echo off
for /f "tokens=1,2,3 delims=^<^>![] " %%a in (file.xml) do (
if defined nextline echo %%c&set nextline=
if "%%a"=="Name" if "%%b"=="%~1" set nextline=1
)
test.cmd DEV2_BUSINESSAPPS_URL
的输出:
https://energy.epipelines.com/businessapps/welcome.html
显然,相应地更改文件名。
答案 1 :(得分:0)
这是xpath.bat -small脚本,它允许您通过xpath表达式获取xml值,而无需使用外部二进制文件:
boolean isFacebook= true;
答案 2 :(得分:0)
这可能是我可能想要做的方式(请参见说明性的rem
注释):
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~1" & rem // (use first command line argument as target XML file)
set "_FIND=%~2" & rem // (use second command line argument as a name to find)
set "_TAG1=Name" & rem // (name of the tag expected in the 1st line of a pair)
set "_TAG2=Value" & rem // (name of the tag expected in the 2nd line of a pair)
rem // Store the name to find in a `for` variable:
for /F "delims= eol=<" %%K in ("%_FIND%") do (
rem // Loop through lines of target XML file, ignore leading white-spaces:
set "PREV=#" & for /F "usebackq tokens=* eol= " %%L in ("%_FILE%") do (
rem // Store current line in variable:
set "LINE=%%L" & setlocal EnableDelayedExpansion
rem // Check whether previous line contains 1st tag and name to find:
if not "!PREV!" == "!PREV:<Name>%%K</Name>=!" (
rem // Check whether current line contains 2nd tag (around the URL):
set "TEST=!LINE:*<Value>=!"
if not "!LINE!" == "!TEST!" if not "!TEST!" == "!TEST:</Value>=!" (
rem // Remove prefix and suffix from URL to extract:
set "TEST=!TEST:*<![CDATA[=|!"
for /F "delims=| eol=|" %%U in ("!TEST:]]>=|!") do (
rem // Actually return the desired URL:
endlocal & echo(%%U
setlocal EnableDelayedExpansion
)
)
)
rem // Store current line in variable for the next loop iteration:
endlocal & set "PREV=%%L"
)
)
endlocal
exit /B