Explore a Document Tree with the xmllint Shell

Explore the tree structure of an XML document with xmllint’s shell mode.

xmllint is a command-line tool available as part of libxml2 (http://xmlsoft.org). It is included in distributions such as Cygwin (http://www.cygwin.com) and Red Hat Linux (http://www.redhat.com). You can also download xmllib2 individually from http://xmlsoft.org. xmllint has an interactive shell mode that lets you traverse an XML document’s tree structure as if it were a file structure, allowing you to examine any node in the tree discretely. Provided that you have an Internet connection, this shell mode will work on remote files as well as local ones. This hack will show you how it’s done.

While in the working directory, we first invoke the shell on an XML document with xmllint:

xmllint --shell time.xml
/ >

A prompt appears (>). The location in the tree is shown to the left of the prompt (/), but with the depth of only one node. Enter the dir command to see information about the document or root node, and follow that with the base command to see the base URI of the document being explored:

            / > dir
DOCUMENT
version=1.0
encoding=UTF-8
URL=time.xml
standalone=true
/ > base
time.xml
/>

Move to a different node with cd, followed by another dir, then by a cat command:

            / > cd time/atomic
            atomic > dir
ELEMENT atomic
  ATTRIBUTE signal
    TEXT
      content=true
atomic > cat
<atomic signal="true"/>
atomic >

A dir gives you information about the node, and cat gives you the XML representation ...

Get XML Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.