0

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

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

you are excused. and i marked as answer and upvoted, i gess what i needed was to know how to only iterate through the child nodes of a, incase node b was also contained within another parent element. danke
Is there just one parent node a in the file? Or may be several of them? Each contents of parent node a is in several separate lines?
Wow, thanks for following up! Never had anyone do that before. Wish i could upvote that answer again, that worked just as I expected it to. Thanks again!
1

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.