Solved Parser XML file using batch to get some specific information

March 30, 2013 at 04:37:29
Specs: Windows 7
For each node list having 'Lists' as its parent node, I want to get/save into a variable values of the first three node, namely 'entry', 'output', and 'token'.
I know do that with vbscript, but it is much more interesting to me to have a solution in batch.
Please, can it be done?

xml file looks like this:
<document>
<dependency>
<GroupId>XX</GroupId>
<ArtifactId>XX</ArtifactId>
<version>XX</version>
</dependency>

<Lists>

<list1>
<entry>myEntry</entry>
<output>myOut</output>
<token>4</token>
<status>1</status>
<number>6</number>
<!-- Comments -->
</list1>

<list2>
<entry>newEntry</entry>
<output>thisOutput</output>
<token>1</token>
<status>0</status>
<number>1</number>
<!-- Comments -->
</list2>

<list3>
dash dash dash dash
</list3>

dash dash dash dash

</Lists>
</document>

Any help!!!
Thanks


See More: Parser XML file using batch to get some specific information

Report •

#1
March 30, 2013 at 10:20:00
It is not a good idea to parse XML or HTML using batch script since batch is not suited to that purpose due to the presence of special characters < and > that are interpreted as redirectors. By cumbersome code the job can be done, but far better to use vbscript.

Report •

#2
March 30, 2013 at 12:40:48
✔ Best Answer
I tend to agree, but I wanted something to do while I had my coffee, so I made this:

@echo off & setlocal
set tags=entry output token
for %%a in (1 2 3) do call :a %%a
for %%a in (%tags%) do set %%a
pause
goto :eof

:a
for /f "tokens=1 delims=:" %%a in ('findstr /r /i /n "<list%1>" xml1') do set sk=%%a
for %%a in (%tags%) do call :x %%a %sk% %1
goto :eof

:x
for /f "tokens=2 delims=<>" %%a in ('more +%2 xml1^| findstr /r /i "<%1>.*</%1>"') do set %1%3=%%a & goto :eof

::===== end
it's slow, due to the loop-structure, but that's batch for ya. Better logic might speed it up, but my coffee is done for today.


Report •
Related Solutions


Ask Question