Skip to Content
XML Hacks
book

XML Hacks

by Michael Fitzgerald
July 2004
Intermediate to advanced
479 pages
12h 30m
English
O'Reilly Media, Inc.
Content preview from XML Hacks

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

.NET & XML

.NET & XML

Niel M. Bornstein
XML Pocket Reference, 3rd Edition

XML Pocket Reference, 3rd Edition

Simon St. Laurent, Michael Fitzgerald

Publisher Resources

ISBN: 0596007116Supplemental ContentErrata Page