I have an application that will only output XML and my destination application can only read plain text files. I have a requirement to split the <pending>
and <results>
tag data it into two files and concatenate the output to a single line. I'm having trouble with that; any help would be appreciated. Yes I would like to keep it in batch script.
Input XML File:
<?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>
Required output(include the trailing space):
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) •
The script I have:
@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
Output I'm getting(in a single file):
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) •
I'm myself a novize in xml, but PowerShell makes it easy:
## 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
Sample output
> 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) •
If you really want to brute force this with a batch file, this may work for you.
@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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.