I have an XML file like this one:
<config>
<intensity>0</intensity>
<variables>
<variable>
<name>TONE</name>
<value>3</value>
<type>UNSIGNED8</type>
</variable>
<variable>
<name>V_SIC</name>
<value>0</value>
<type>UNSIGNED8</type>
</variable>
<variable>
<name>H_YPE</name>
<value>9000</value>
<type>INTEGER16</type>
</variable>
<variable>
<name>NUM_SIC</name>
<value>0</value>
<type>UNSIGNED8</type>
</variable>
</variables>
</config>
I'm trying to get the value text from the node with the name H_YPE
into a variable, so in this example, the variable should have 9000
. If the node H_YPE
is not on the XML, simply store a 1.
I saw This answer and tried what Magoo
implements, and while I can see that the values are being parsed, I'm interested in obtaining just the one value and storing it in a variable for later use.
Apparently I'm not very savvy with batch scripts, so any and all help with this would be appreciated, thanks in advance.
CodePudding user response:
A batch file processed by the Windows Command Processor cmd.exe
is the worst choice for processing an XML file because there is no built-in support for real XML processing.
The following batch file code could be used if the XML file is always structured as shown in the question.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "Value=1"
if not exist "File.xml" goto ProcessValue
set "NameTagFound="
for /F "tokens=2,3 delims=<>" %%I in ('%SystemRoot%\System32\findstr.exe /R "<name>H_YPE</name> <value>[0123456789]*</value>" "File.xml"') do (
if "%%I" == "name" (
if /I "%%J" == "H_YPE" set "NameTagFound=1"
) else if defined NameTagFound (
if "%%I" == "value" (
if not "%%J" == "" set "Value=%%J"
goto ProcessValue
)
)
)
:ProcessValue
echo Value is: %Value%
endlocal
Please note following requirements for the structure of the data in the XML file:
- There must be leading spaces/tabs left to the start tags
<name>
and<value>
because oftokens=2,3 delims=<>
and the two elements must be on separate lines not containing any other element or attributes. - An element
name
with valueH_YPE
must exist only within elementvariable
and not within another element because of the code does not check where<name>H_YPE</name>
is found in the file. - The element
name
with valueH_YPE
must be inside the elementvariable
on a separate line above the line with the elementvalue
. So the order of the elementsname
andvalue
cannot be changed without adapting the code. - The element
variable
with the elementname
with valueH_YPE
must have also the elementvalue
or the value of next elementvalue
found anywhere below the line with<name>H_YPE</name>
is assigned wrongly to the environment variableValue
instead of using the default value1
. - The value
H_YPE
must be always in the XML file with this spelling (all letters in upper case).
The code could be enhanced to do more XML structure checks if one of the five requirements is not always fulfilled by the XML file contents.
XML tags are case-sensitive according to XML specification. Therefore this code runs FINDSTR also case-sensitive although the value H_YPE
could be in any case per XML specification. The IF conditions comparing strings are also case-sensitive for the same reason including the IF condition which checks for value H_YPE
.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
echo /?
endlocal /?
findstr /?
for /?
goto /?
if /?
set /?
setlocal /?