You could try xmllint

The xmllint program parses one or more XML files, specified on the command line as xmlfile. It prints various types of output, depending upon the options selected. It is useful for detecting errors both in XML code and in the XML parser itse

It allows you select elements in the XML doc by xpath, using the --pattern option.

On Mac OS X (Yosemite), it is installed by default.
On Ubuntu, if it is not already installed, you can run apt-get install libxml2-utils

Answer from Joel on Stack Overflow
Top answer
1 of 10
102

You could try xmllint

The xmllint program parses one or more XML files, specified on the command line as xmlfile. It prints various types of output, depending upon the options selected. It is useful for detecting errors both in XML code and in the XML parser itse

It allows you select elements in the XML doc by xpath, using the --pattern option.

On Mac OS X (Yosemite), it is installed by default.
On Ubuntu, if it is not already installed, you can run apt-get install libxml2-utils

2 of 10
25

Here's a fully working example.

If it's only extracting email addresses you could do something like:

  1. Suppose XML file spam.xml is like

    <spam>
    <victims>
      <victim>
        <name>The Pope</name>
        <email>[email protected]</email>
        <is_satan>0</is_satan>
      </victim>
      <victim>
        <name>George Bush</name>
        <email>[email protected]</email>
        <is_satan>1</is_satan>
      </victim>
      <victim>
        <name>George Bush Jr</name>
        <email>[email protected]</email>
        <is_satan>0</is_satan>
      </victim>
    </victims>
    </spam>
    
  2. You can get the emails and process them with this short bash code:

    #!/bin/bash
    emails=($(grep -oP '(?<=email>)[^<]+' "/my_path/spam.xml"))
    
    for i in ${!emails[*]}
    do
      echo "$i" "${emails[$i]}"
      # instead of echo use the values to send emails, etc
    done
    

The result of this example is:

0 [email protected]
1 [email protected]
2 [email protected]

Important note:
Don't use this for serious matters. This is OK for playing around, getting quick results, learning to grep, etc. but you should definitely look for, learn and use an XML parser for production (see Micha's comment below).

🌐
Opensource.com
opensource.com › article › 21 › 7 › parse-xml-linux
Use XMLStarlet to parse XML in the Linux terminal | Opensource.com
July 16, 2021 - $ xmlstarlet select --template \ --value-of /xml/os/linux/distribution \ --nl myfile.xml Fedora 7 Moonshine Live Fedora Everything Fedora Core 6 Zod · The --nl stands for "new line," and it inserts copious amounts of whitespace to ensure your terminal prompt gets a new line after your results are in.
Top answer
1 of 9
26

Using bash and xmllint (as given by the tags):

xmllint --version  #  xmllint: using libxml version 20703

# Note: Newer versions of libxml / xmllint have a --xpath option which 
# makes it possible to use xpath expressions directly as arguments. 
# --xpath also enables precise output in contrast to the --shell & sed approaches below.
#xmllint --help 2>&1 | grep -i 'xpath'

{
# the given XML is in file.xml
host="$(echo "cat /config/global/resources/default_setup/connection/host/text()" | xmllint --nocdata --shell file.xml | sed '1d;$d')"
username="$(echo "cat /config/global/resources/default_setup/connection/username/text()" | xmllint --nocdata --shell file.xml | sed '1d;$d')"
password="$(echo "cat /config/global/resources/default_setup/connection/password/text()" | xmllint --nocdata --shell file.xml | sed '1d;$d')"
dbname="$(echo "cat /config/global/resources/default_setup/connection/dbname/text()" | xmllint --nocdata --shell file.xml | sed '1d;$d')"
printf '%s\n' "host: $host" "username: $username" "password: $password" "dbname: $dbname"
}

# output
# host: localhost
# username: root
# password: pass123
# dbname: testdb

In case there is just an XML string and the use of a temporary file is to be avoided, file descriptors are the way to go with xmllint (which is given /dev/fd/3 as a file argument here):

set +H
{
xmlstr='<?xml version="1.0"?>
<config>
    <global>
        <install>
            <date><![CDATA[Tue, 11 Dec 2012 12:31:25 +0000]]></date>
        </install>
        <crypt>
            <key><![CDATA[70e75d7969b900b696785f2f81ecb430]]></key>
        </crypt>
        <disable_local_modules>false</disable_local_modules>
        <resources>
            <db>
                <table_prefix><![CDATA[]]></table_prefix>
            </db>
            <default_setup>
                <connection>
                    <host><![CDATA[localhost]]></host>
                    <username><![CDATA[root]]></username>
                    <password><![CDATA[pass123]]></password>
                    <dbname><![CDATA[testdb]]></dbname>
                    <initStatements><![CDATA[SET NAMES utf8]]></initStatements>
                    <model><![CDATA[mysql4]]></model>
                    <type><![CDATA[pdo_mysql]]></type>
                    <pdoType><![CDATA[]]></pdoType>
                    <active>1</active>
                </connection>
            </default_setup>
        </resources>
        <session_save><![CDATA[files]]></session_save>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <frontName><![CDATA[admin]]></frontName>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>
'

# exec issue
#exec 3<&- 3<<<"$xmlstr"
#exec 3<&- 3< <(printf '%s' "$xmlstr")
exec 3<&- 3<<EOF
$(printf '%s' "$xmlstr")
EOF

{ read -r host; read -r username; read -r password; read -r dbname; } < <(
       echo "cat /config/global/resources/default_setup/connection/*[self::host or self::username or self::password or self::dbname]/text()" | 
          xmllint --nocdata --shell /dev/fd/3 | 
          sed -e '1d;/d'
       )

printf '%s\n' "host: $host" "username: $username" "password: $password" "dbname: $dbname"

exec 3<&-
}
set -H


# output
# host: localhost
# username: root
# password: pass123
# dbname: testdb
2 of 9
14

Using xmllint and the --xpath option, it is very easy. You can simply do this:

XML_FILE=/path/to/file.xml

HOST=$(xmllint --xpath 'string(/config/global/resources/default_setup/connection/host)' $XML_FILE
USERNAME=$(xmllint --xpath 'string(/config/global/resources/default_setup/connection/username)' $XML_FILE
PASSWORD=$(xmllint --xpath 'string(/config/global/resources/default_setup/connection/password)' $XML_FILE 
DBNAME=$(xmllint --xpath 'string(/config/global/resources/default_setup/connection/dbname)' $XML_FILE

If you need to get to an element's attribute, that's also easy using XPath. Imagine you have the file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="screensaver.turnoff"
       name="Turn Off"
       version="0.10.0"
       provider-name="Dag Wieërs">
  ..snip..
</addon>

The needed shell statements would be:

VERSION=$(xmllint --xpath 'string(/addon/@version)' $ADDON_XML)
AUTHOR=$(xmllint --xpath 'string(/addon/@provider-name)' $ADDON_XML)
🌐
Baeldung
baeldung.com › home › files › file editing › xmllint in linux
xmllint in Linux | Baeldung on Linux
March 18, 2024 - On the other hand, in RHEL based Linux (such as CentOS), we’ll need to install the xmlstarlet package using yum: $ sudo yum update -qq $ sudo yum install -y xmlstarlet · Generally, we run the xmllint command with a list of optional flags and one or more XML file paths at the end: ... Let’s look at some use cases for xmllint.
🌐
Linux Hint
linuxhint.com › parse-xml-linux-command-line
How to Parse XML on Linux Command Line – Linux Hint
A guide on how to parse XML on the Linux Command Line by focusing on two options for parsing XML files, using the xmllint command and the XMLStarlet command.
🌐
IT'S FOSS
itsfoss.gitlab.io › post › how-to-parse-or-view-xml-code-in-linux-command-line
How to Parse or View XML Code in Linux Command Line :: IT'S FOSS
August 11, 2025 - Parse view XML code on your Linux command line. Learn how to pretty-print XML files with simple effective commands.
🌐
UbuntuMint
ubuntumint.com › home › linux commandline tips › how to parse or view xml code in linux command line
How to Parse or View XML Code in Linux Command Line
July 24, 2023 - Out of the many approaches to formatting, printing, and outputs an XML file on the Linux terminal, we will look into two ideal solutions: The xmllint command is part of the xmllib2 package with a primary role of checking XML files validity, ...
🌐
Ubuntu Manpages
manpages.ubuntu.com › manpages › focal › man1 › xmllint.1.html
Ubuntu Manpage: xmllint - command line XML tool
The xmllint program parses one or more XML files, specified on the command line as · XML-FILE (or the standard input if the filename provided is - ). It prints various types of output, depending upon the options selected. It is useful for detecting errors both in XML code and in the XML parser ...
Find elsewhere
🌐
Linux Today
linuxtoday.com › home › developer
How to Parse or View XML Code in Linux Command Line | Linux Today
April 28, 2022 - XML is an abbreviation for Extensible Markup Language. Learn how to parse or view XML code in the Linux command line through this tutorial.
🌐
Wikihow
wikihow.com › computers and electronics › operating systems › linux › 4 easy ways to run an xml file on linux
4 Easy Ways to Run an XML File on Linux
November 7, 2023 - On most other distributions, you'll need to install it from your package manager. On Ubuntu, Debian, and Linux Mint, use the command sudo apt install xmlstarlet to install the package.
🌐
Linux.org
linux.org › docs › man1 › xmllint.html
xmllint - command line XML tool at Linux.org
--push Use the push mode of the parser. --recover Output any parsable portions of an invalid document. --relaxng SCHEMA Use RelaxNG file named SCHEMA for validation. --repeat Repeat 100 times, for timing or profiling. --schema SCHEMA Use a W3C XML Schema file named SCHEMA for validation. --shell Run a navigating shell. Details on available commands in shell mode are below (see the section called “SHELL COMMANDS”).
🌐
GitHub
github.com › sibprogrammer › xq
GitHub - sibprogrammer/xq: Command-line XML and HTML beautifier and content extractor
The JSON output will be an object where: ... The preferable ways to install the utility are described below. ... A more detailed list of Linux distros that package the xq utility can be found here: https://repology.org/project/xq-sibprogram...
Starred by 1.1K users
Forked by 33 users
Languages   Go 92.6% | HTML 4.7% | Shell 2.2% | Dockerfile 0.5% | Go 92.6% | HTML 4.7% | Shell 2.2% | Dockerfile 0.5%
🌐
TheLinuxCode
thelinuxcode.com › home › how to parse and process xml files from the linux command line
How to Parse and Process XML Files from the Linux Command Line – TheLinuxCode
November 12, 2023 - For more complex updates, you may ... an XML parser in Python, Ruby, etc. But xmlstarlet works great for quick command line modifications. Let‘s walk through a real-life example using some of these tools together to: ... This demonstrates how these tools can be combined to go from an XML feed all the way to your database without writing any code! Here are some other handy tips for working with XML from the Linux command ...
🌐
LiveJournal
kolya-iks.livejournal.com › 49403.html
bash xml parsing: kolya_iks — LiveJournal
xmllint — command line XML tool to parse xml files example of usage: 1.
🌐
Wikipedia
en.wikipedia.org › wiki › XMLStarlet
XMLStarlet - Wikipedia
September 8, 2025 - XMLStarlet is a set of command line utilities (toolkit) to query, transform, validate, and edit XML documents and files using a simple set of shell commands in a way similar to how it is done with UNIX grep, sed, awk, diff, patch, join, etc commands.
🌐
Linux Man Pages
linux.die.net › man › 1 › xmllint
xmllint(1): XML tool - Linux man page
The xmllint program parses one or more XML files, specified on the command line as XML-FILE (or the standard input if the filename provided is - ). It prints various types of output, depending upon the options selected. It is useful for detecting errors both in XML code and in the XML parser itself.
🌐
Meld
gnome.pages.gitlab.gnome.org › libxml2 › xmllint.html
xmllint
The xmllint program parses one or more XML files, specified on the command line as XML-FILE (or the standard input if the filename provided is - ). It prints various types of output, depending upon the options selected. It is useful for detecting errors both in XML code and in the XML parser itself.