Home > other >  Obtain particular node tag value from XML with batch
Obtain particular node tag value from XML with batch

Time:10-11

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:

  1. There must be leading spaces/tabs left to the start tags <name> and <value> because of tokens=2,3 delims=<> and the two elements must be on separate lines not containing any other element or attributes.
  2. An element name with value H_YPE must exist only within element variable and not within another element because of the code does not check where <name>H_YPE</name> is found in the file.
  3. The element name with value H_YPE must be inside the element variable on a separate line above the line with the element value. So the order of the elements name and value cannot be changed without adapting the code.
  4. The element variable with the element name with value H_YPE must have also the element value or the value of next element value found anywhere below the line with <name>H_YPE</name> is assigned wrongly to the environment variable Value instead of using the default value 1.
  5. 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 /?
  • Related