Batch – get tag value from XML file

Issue

I have this following XML file called “test.xml”

how can I get “Agent-name-8181818” value from “ragent-name” tag into a variable within a batch file?

<ragent>
    <type>ragent</type>
    <logger>
        <loglevel>log_warning</loglevel>
        <logger-dir>.</logger-dir>
    </logger>
    <additional-config>
        <logger-level>log_warning</logger-level>
    </additional-config>
    <configuration-info ts="26-02-2018_15-31-54">
        <apply-config-now>false</apply-config-now>
        <manual-settings-activation>Automatic</manual-settings-activation>
        <ragent-name ts="26-02-2018_15-31-54">Agent-name-8181818</ragent-name>
        <site ts="26-02-2018_15-31-54">site</site>
    </configuration-info>
</ragent>

Solution

With xpath.bat (does not require external binaries , just uses built-in windows capabilities):

call xpath test.xml "*//configuration-info/ragent-name"

to assign result to a variable:

for /f "tokens=* delims=" %%# in ('call xpath test.xml "*//configuration-info/ragent-name"') do set "reagent=%%#"

Answered By – npocmaka

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published