Working with XML

How to:

Here’s how to parse XML in Bash. Tools? xmllint and xmlstarlet. Looping through XML elements? Definitely. Example with sample output:

# Assuming xmlstarlet is installed
# Install with: apt-get install xmlstarlet

# Parsing XML content
cat <<EOF > sample.xml
  <fruit name="Apple"/>
  <fruit name="Banana"/>

# Extract names with xmlstarlet
xmlstarlet sel -t -m "//fruit" -v "@name" -n sample.xml

# Output should be:
# Apple
# Banana

Deep Dive

Back in the ’90s, XML popped up as a simpler alternative to SGML, but more structured than HTML. Now, it’s got company – JSON, YAML, for instance. But XML’s still kicking, especially in configs and SOAP-based web services.

Tool-wise, xmllint is comfy for XML validation, xpath queries. xmlstarlet is the swiss-army knife for XML shenanigans – query, edit, validate, transform. In bash scripts, they’re superheroes for XML tasks.

Under the hood, xmllint uses libxml2 – the XML C parser. It’s fast, but the error messages? Cryptic. And xmlstarlet? Recursive templates and the EXSLT support. Mind bender, but powerful.

See Also