Skip to content

Latest commit

 

History

History
85 lines (57 loc) · 3.5 KB

README.md

File metadata and controls

85 lines (57 loc) · 3.5 KB

xmlshow: A super-simple and powerful viewer for large XML files, for the command-line

Introduction

Ever found yourself with an XML file just too large for your editor or your browser, and did you (like me) spent your time trying to find a simple solution on the Web? This one may be for you.

And if it isn't, you may have found enough material to create your own, just with standard utilities.

Features

xmlshow is a super-simple XML viewer for the Unix command line, which should provide most everything you could wish, since uses the features of less.

  • It will display any large XML file in a breeze.
  • It has syntax highlighting
  • If it is a zip file, it will unzip it in the process (zipping is both indispensable and effective, since XML is wonderfully redundant).
  • It has line numbering
  • You can navigate forward and backward
  • You can use sed search patterns.

Limitations

There are two things, however, that it won't do:

  • Modifying the file
  • Prepare or serve coffee

Usage

Just type:

$ xmlshow <filename>

It will also read from stdin if no argument is provided. Hence if some utility outputs some well-form XML on stdout, you can pipe it into xml show:

$ some-utility | xmlshow

How it works

It combines:

  • unzip, to extract the file
  • file to check the mime type of the input file
  • xmllint, for beautifying the XML.
  • highlight to highlight the syntax
  • less to view the file and add the line numbers.

The script does it for you, but this is the substance:

 xmllint --format <yourfile> | highlight --syntax=xml --out-format=xterm-256 | less -R -N

(the -R parameter in less is for raw (i.e. to preserve the escape codes for colors), and -N is for numbering)

That's all there is to it: it's just a typical shell hack with Unix pipes, sufficiently long to warrant a script file, and sufficiently useful to be put on github.

How to install it

  1. Download the .sh file an install it some directory in your path (type echo $PATHto find out).
  2. Make it executable: chmod +x xmlshow.sh
  3. In that same directory, create a soft link: ln -s xmlshow.sh xmlshow
  4. If it fails, make sure that utilities mentioned above (typically xmllint and highlight) are installed on your system. If not use your favorite package manager

Voilà, you should be all set.

Tips

Navigating the file

The less utility has actually quite a few tricks up its sleeves.

Beside the usual commands, remember that:

  • zjumps one window forward
  • wjumps one window backward
  • h calls a help, with plenty of good features you may not have suspected (use q to quit)
  • To search for a pattern just type /pattern

Viewing more text

It is also worth remembering that it is often easy to display more text at once, either by getting a smaller font on your terminal pane (on MacOS: CMD -, CMD 0 to get to normal), or by increasing the size of the window or pane!

Consulting the XML content of a docx document

Since you can use xmlshow in a pipe, you can do something like this:

unzip -p myfile.docx word/document.xml | xmlshow

It tells unzip to decompress the document.xml file and to send it to stdout, ready to be piped into xmlshow.