
xmllint (on Linux) can be used the parse the XML file. The which command can be used to determine if xmllint is found in your $PATH.
which xmllint
If xmllint is in your $PATH, something like this should be returned.
/usr/bin/xmllint
If xmllint is not in your $PATH, xmllint is most likely not installed. In this scenario, apt-get or yum can be used to install xmllint.
yum install xmllint
Let's say you have an XML file named food.xml that contains the following.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<name>apple</name>
<type>fruit</type>
</root>
The xmllint command followed by an XML file can be used to determine if the XML file has any syntax errors. Often, the --noout flag is included so that the content of the XML file is not outputted.
xmllint food.xml --noout
Then $? can be used to determine if the xmllint command returned 0 (success) or 1 (failed).
echo $?
Here is how you would return the <name> tag.
xmllint --xpath //root//name food.xml
Or like this.
xmllint --xpath //name food.xml
Which should return the following.
<name>apple</name>
Here is how you would return the value in the <name> tag.
xmllint --xpath "string(//name)" food.xml
Which should return the following.
apple
If you have an XSD file, the --schema option can be used to validate the XML file against the XSD file.
xmllint --schema foo.xsd bar.xml --noout
Did you find this article helpful?
If so, consider buying me a coffee over at