jeudi 26 mars 2015

Get all strings between a specific tag in an unformatted XML file with a batch file

I'm trying to get the strings between 2 tags in an XML file adapting a solution I found in here.


This is the batch file I've:



@echo off
setlocal EnableDelayedExpansion

(for /F "delims=" %%a in ('findstr /I /L "<Name>" contacts.xml') do (
set "line=%%a
set "line=!line:*<Name>=!"
for /F "delims=<" %%b in ("!line!") do echo %%b
)) > list.txt


Now when the XML is formatted I get all the names



<List>
<Contacts>
<Row>
<Name>Carlos</Name>
<Path>\Some\path\1</Path>
<Hidden>False</Hidden>
</Row>
<Row>
<Name>Fernando</Name>
<Path>\Some\path\2</Path>
<Hidden>False</Hidden>
</Row>
<Row>
<Name>Luis</Name>
<Path>\Some\path\3</Path>
<Hidden>False</Hidden>
</Row>
<Row>
<Name>Daniel</Name>
<Path>\Some\path\4</Path>
<Hidden>False</Hidden>
</Row>
</Contacts>
</List>



Carlos


Fernando


Luis


Daniel



But when the XML(This is how it's generated) is in 1 line I only get the first name



<List><Contacts><Row><Name>Carlos</Name><Path>\Some\path\1</Path><Hidden>False</Hidden><Row><Name>Carlos</Name><Path>\Some\path\1</Path><Hidden>False</Hidden></Row><Row><Name>Carlos</Name><Path>\Some\path\1</Path><Hidden>False</Hidden></Row><Row><Name>Carlos</Name><Path>\Some\path\1</Path><Hidden>False</Hidden></Row></Contacts></List>



Carlos



What changes should I make to the batch file so it correctly parse unformatted XML files?


Aucun commentaire:

Enregistrer un commentaire