Suppose you have following XML file:

<?xml version="1.0"?>

<configuration>
    <parameter>
        <name>param1</name>
        <value>value1</value>
    </parameter>
    <parameter>
        <name>param2</name>
        <value>value2</value>
    </parameter>
    <parameter>
        <name>param3</name>
        <value>value3</value>
    </parameter>
</configuration>

And you need to get param2’s value. In bash (or other shell) you can use xmllint tool:

xmllint ---shell conf.xml <<<"<xmllint_command> <XPath_path>"

For example:

xmllint ---shell conf.xml <<<"cat /configuration/parameter[name='param2']/value/text()"

This will output the desired value among some shell prompts, so we will need to filter out that further:

xmllint ---shell conf.xml <<<"cat /configuration/parameter[name='param2']/value/text()" | grep -v "^/ >"

This will only output the desired value.

xmllint supports more commands and XPath is more flexible but this is out of scope of this tool tip.

Leave a Reply

Close Menu