Solved Read specific content of an XML file through Batch File

March 20, 2014 at 23:45:47
Specs: Windows 7
I am trying to read the content of XML file to get a particular value from a tag and export in another .txt or .xls file. The XML file looks like:

<definition name=".test1" extends=".b">
<put name="id" value="TR012" />
</definition>

<definition name=".test2" extends=".b">
<put name="id" value="TR013" />
</definition>

The XML file has multiple such entries and i want to read:
1) the value in name property of definition tag
2) the value in value property of <put> tag

Currently when i tried i got only the last value and not all of them. Is it possible to retrieve internal specific values and export to another file through BAT file?
Need Help on this piece


See More: Read specific content of an XML file through Batch File

Report •


#1
March 20, 2014 at 23:57:48
Post your current script (even though it isn't workng). might be fixable.

Report •

#2
March 21, 2014 at 00:01:47
@echo off & setlocal enabledelayedexpansion
for /f "delims=" %%a in ('findstr /C:"name=" C:\\test.xml') do set content=%%a
set content=%content:*"=%
set content=%content:~0,-1%
echo %content%
pause > nul
exit /b

Report •

#3
March 21, 2014 at 02:19:46
✔ Best Answer
@echo off & setlocal
for /f "tokens=1,2,3* delims==" %%a in (C:\\test.xml) do (
if /i "%%a" equ "<definition name" call :zz %%b
if /i "%%a" equ "<put name" call :zz "%%~c"
)
goto :eof

:zz
echo %~1
::=== not fully test, might need tweaked.


Report •

Related Solutions

#4
March 21, 2014 at 08:31:43
Sorry but this one does not give any results as output....

Report •

#5
March 21, 2014 at 19:30:22
All I had to test with was your sample data. The script, applied to that, delivered the goods, on win-XP. I used my own filename for the test data, and not "C:\\test.xml" - that is the only difference from my posted script.

Report •

#6
March 22, 2014 at 14:02:32

Is there a need for an escape?
/f "tokens=1,2,3* delims=^=" %%a in (C:\\test.xml) do (

I have never used an = as a delims, but I was thinking for for /f you had to escape..

::mike


Report •

#7
March 22, 2014 at 20:28:24
Hi Mike: nah, "=" does not need escaped, but that was a good point. (I always have trouble remembering what special char.s need it and which don't, so I always have to test if in doubt). It worked either way, both on XP and seven.

Report •

#8
March 23, 2014 at 01:39:39
Thanks nbrane! I always pick up new things following your posts.

::mike


Report •


Ask Question