O'Reilly logo

XML Hacks by Michael Fitzgerald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required