Reputation: 4685
if i have the following xml:
<a>
<b>valA</b>
<b>valB</b>
</a>
<c>
<b>valA</b>
<b>valB</b>
</c>
and the following cmd:
for /f "delims=" %%f in ('dir /b /s server.xml') do (
FOR /F "tokens=2 delims=>" %%i in ('findstr "<a>" %%f') do @echo %%i > temp1.txt
FOR /F "tokens=1 delims=<" %%i in (temp1.txt) do @echo %%i
)
i need to access the values of node b but the above code only will work if i have values like this
<a>asdf</a>
I only want to iterate through parent element a
Upvotes: 0
Views: 7064
Reputation: 57252
Though you'll need to add a root element to make the xml valid you can check the xpath.bat:
call xpath.bat "server.xml" "//b"
Upvotes: 1
Reputation: 67216
Excuse me. I think I really don't understand what you need, but the Batch file below show the values of node b in server.xml file:
for /F "tokens=3 delims=<>" %%i in ('findstr "<b>" server.xml') do echo %%i
Result:
valA
valB
Is this what you want?
EDIT: New version added in accordance with the additional comment
@echo off
setlocal EnableDelayedExpansion
set fileName=server.xml
findstr /N "<b>" %fileName% > nodesB.tmp
call :seekNodesA < nodesB.tmp
del nodesB.tmp
goto :EOF
:seekNodesA
set lineB=0
set startLine=
for /F "delims=:" %%a in ('findstr /N "a>" %fileName%') do (
if not defined startLine (
set startLine=%%a
) else (
call :checkNodeB !startLine! %%a
set startLine=
)
)
exit /B
:checkNodeB startNodeA endNodeA
if %lineB% gtr %1 goto showNodeB
set nodeB=99999999:
set /P nodeB=
for /F "tokens=1* delims=:" %%b in ("%nodeB%") do (
set lineB=%%b
set "nodeB=%%c"
)
goto checkNodeB
:showNodeB
if %lineB% gtr %2 exit /B
for /F "tokens=3 delims=<>" %%i in ("%nodeB%") do echo %%i
set nodeB=99999999:
set /P nodeB=
for /F "tokens=1* delims=:" %%b in ("%nodeB%") do (
set lineB=%%b
set nodeB=%%c
)
goto showNodeB
Given this data file:
<a>
<b>valB1-A</b>
<b>valB2-A</b>
</a>
<c>
<b>valB1-C</b>
<b>valB2-C</b>
</c>
<a>
<b>valB3-A</b>
<b>valB4-A</b>
</a>
Previous Batch file get this result:
valB1-A
valB2-A
valB3-A
valB4-A
I assumed several details.
Upvotes: 6