我有一个仅会输出XML的应用程序,而我的目标应用程序只能读取纯文本文件。我需要将<pending>
和<results>
标签数据拆分为两个文件,并将输出连接到一行。我遇到了麻烦;任何帮助,将不胜感激。是的,我想将其保留在批处理脚本中。
输入XML文件:
<?xml version="1.0" encoding="utf-8"?>
<game>
<visible>true</visible>
<pending>
<item>W1: Player 5 versus Player 28 • </item>
<item>W1: Player 12 versus Player 21 • </item>
<item>W1: Player 2 versus Player 31 • </item>
</pending>
<results>
<item>W1: Player 13 defeats Player 20 (21-8) • </item>
<item>W1: Player 29 defeats Player 4 (15-21) • </item>
<item>W1: Player 24 defeats Player 9 (7-21) • </item>
</results>
<time>636680475521951568</time>
</game>
必需的输出(包括尾随空格):
Pending.txt
W1: Player 5 versus Player 28 • W1: Player 12 versus Player 21 • W1: Player 2 versus Player 31 •
Results.txt
W1: Player 13 defeats Player 20 (21-8) • W1: Player 29 defeats Player 4 (15-21) • W1: Player 24 defeats Player 9 (7-21) •
我拥有的脚本:
@echo OFF
for /f "delims=" %%i in ('findstr /i /c:"<item>" input.xml') do call :job "%%i"
goto :eof
:job
set line=%1
set line=%line:/=%
set line=%line:<=+%
set line=%line:>=+%
set line=%line:*+item+=%
set line=%line:+=&rem.%
echo.%line%>>output.txt
:eof
我得到的输出(在一个文件中):
W1: Player 5 versus Player 28 •
W1: Player 12 versus Player 21 •
W1: Player 2 versus Player 31 •
W1: Player 13 defeats Player 20 (21-8) •
W1: Player 29 defeats Player 4 (15-21) •
W1: Player 24 defeats Player 9 (7-21) •
答案 0 :(得分:1)
我本人是xml的新手,但是PowerShell使它变得容易:
## Q:\Test\2018\07\25\SO_51509117.ps1
[xml]$xml=Get-Content .\input.xml -encoding utf8
($xml.game.pending.item|ForEach-Object{$_} ) -join '' |Set-Content .\pending.txt -Encoding utf8
($xml.game.results.item|ForEach-Object{$_} ) -join '' |Set-Content .\results.txt -Encoding utf8
样本输出
> gc .\pending.txt
W1: Player 5 versus Player 28 • W1: Player 12 versus Player 21 • W1: Player 2 versus Player 31 •
> gc .\results.txt
W1: Player 13 defeats Player 20 (21-8) • W1: Player 29 defeats Player 4 (15-21) • W1: Player 24 defeats Player 9 (7-21) •
答案 1 :(得分:0)
如果您真的想用批处理文件强行使用它,这可能对您有用。
@echo off
set "results="
set "pending="
FOR /F "tokens=1* delims=<> " %%G IN (input.xml) do (
IF /I "%%G"=="/pending" set "pending="
IF DEFINED PENDING (
FOR /F "tokens=1 delims=<" %%I IN ("%%H") DO call set "pline=%%pline%%%%I
)
IF /I "%%G"=="pending" set "pending=0"
IF /I "%%G"=="/results" set "results="
IF DEFINED RESULTS (
FOR /F "tokens=1 delims=<" %%I IN ("%%H") DO call set "rline=%%rline%%%%I
)
IF /I "%%G"=="results" set "results=0"
)
echo %pline%
echo %rline%
pause