在 Windows 批处理中解析 XML 来获取标记属性

问题描述 投票:0回答:1

我一直在寻找这个。并看过很多关于此的页面,但似乎没有一个对我有用。其中许多都有我无法使用的标记,因为我的属性“文件名”可能位于标签中的任何位置。

<?xml version="1.0" encoding="UTF-8" ?>
<thisprog>
<sessions>
    <main>
        <File fvl="0" offst="0" filename="c:\here\there.txt" backupFilePath="" />
        <File filename="c:\here\there2.txt" mapWrapIndentMode="-1" wrappie="no" />
        <File wrapCount="1" filename="c:\here\folder1\oops.txt" wrappie="no" />
        <File lang="None" encd="-1" ro="no" filename="c:\here\folder 2\foop.txt" />
    </main>
</sessions>
</thisprog>

我正在尝试让批处理获取“文件名”属性并将它们连接在一起。所以最后是(和引号,因为它们是目录并且可能有空格):

"c:\here\there.txt" "c:\here\there2.txt" "c:\here\folder1\oops.txt" "c:\here\folder 2\foop.txt"

我在这里尝试了代码属性撕裂,但我无法连接文件名字符串。我一定是错过了什么。

@echo off&setlocal

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "XML=%~1"
set "TAG=%~2"
set "PAR=%~3"
set "ALLFILES="

rem // Define defaults here:
if not defined XML set "XML=presentation.xml"
if not defined TAG set "TAG=slideshow"
if not defined PAR set "PAR=date"

set "FLAG="
for /F usebackq^ delims^=^ eol^= %%L in ("%XML%") do (
    set "LINE=%%L"
    setlocal EnableDelayedExpansion
    set "REST=!LINE:*<%TAG%=!"
    if not defined REST (
        set "FLAG=#"
    ) else (
        set "REST=!LINE:*<%TAG% =!"
        if not "!REST!"=="!LINE!" (
            set "FLAG=#"
        ) else (
            set "REST=!LINE:*<%TAG% =!"
            if not "!REST!"=="!LINE!" (
                set "FLAG=#"
            )
        )
    )
    for /F "tokens=1,2 delims=>" %%E in ("!REST!/") do (
        if defined FLAG (
            endlocal
            set "FLAG=#"
        ) else (
            endlocal
        )
        set "REST=%%E"
        if defined FLAG (
            call :GET_ATTR "REST:~,-1" "%PAR%"
        )
        if not "%%F"=="" (
            set "FLAG="
        )
        setlocal EnableDelayedExpansion
    )
    endlocal
)
echo "the end"
echo %ALLFILES%
endlocal
exit /B


:GET_ATTR var_string param_name
setlocal DisableDelayedExpansion
set "PAR=%~2"
setlocal EnableDelayedExpansion
set "STR=!%~1!"
set "ALLFILESFUNC=%%~S "
set "NEXT="
for %%S in (!STR!) do (
    if defined NEXT (
        endlocal
        echo the filename - "%%~S"
        rem //This is the filename attribute but I can't get it to concatenate to an accumulative string
        rem echo(%%~S
        set "NEXT="
        rem //It's not collecting the strings.
        set "ALLFILESFUNC=!ALLFILESFUNC! "^%%~S"^ "
        echo Accumulative - %ALLFILESFUNC%
        setlocal EnableDelayedExpansion
    )
    set "STR=!STR:*%%S=!"
    if "%%S"=="%PAR%" (
        if defined STR (
            if "!STR:~,1!"=="=" (
                if "!STR:~1,1!"==" " (
                    echo(
                ) else if "!STR:~1,1!"=="   " (
                    echo(
                ) else (
                    set "NEXT=#"
                )
            )
        )
    )
)
if defined NEXT echo(
endlocal
endlocal
exit /B

如有任何帮助,我们将不胜感激。

更新:我让它获取文件名,但我不会将所有文件名收集到一个变量 ALLFILESFUNC 中。

xml windows batch-file command
1个回答
0
投票

恐怕我不明白你的代码...

这就是我会做的方式:

@echo off
setlocal EnableDelayedExpansion

set "AllFiles="
(for /F "tokens=1*" %%a in ('findstr "filename=" test.txt') do (
   set "line=%%b"
   for /F "delims=" %%c in (^"!line:" ="^

!^") do set %%c
   set "AllFiles=!AllFiles! !filename!"
)) 2> NUL
echo AllFiles=%AllFiles%

输出:

AllFiles= "c:\here\there.txt" "c:\here\there2.txt" "c:\here\folder1\oops.txt" "c:\here\folder 2\foop.txt"
© www.soinside.com 2019 - 2024. All rights reserved.