How can the shell access values in an XML file?

One way to extract values from an XML in shell is to use the xmllint tool, which is a powerful XML parser and validator.

Firstly, make sure that the xmllint tool is installed on your system. You can check if it’s installed by using the following command:

xmllint --version

Next, use the following command to retrieve the specified value from the XML file:

xmllint --xpath 'XPath表达式' 文件名.xml

One of these is the XPath expression, which is used to extract values from XML, where “filename.xml” is the name of the file containing XML data.

For example, suppose there is an XML file named data.xml with the following content:

<root>
    <person>
        <name>John</name>
        <age>30</age>
    </person>
</root>

You can use the following command to extract the value of the name element:

xmllint --xpath 'string(/root/person/name)' data.xml

The output will be John.

To extract the value of the age element, you can use the following command:

xmllint --xpath 'string(/root/person/age)' data.xml

The output will be 30.

Different XPath expressions can be used to extract other values from the XML as needed.

bannerAds