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.
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.
There are two things, however, that it won't do:
- Modifying the file
- Prepare or serve coffee
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
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.
- Download the .sh file an install it some directory in your path (type
echo $PATH
to find out). - Make it executable:
chmod +x xmlshow.sh
- In that same directory, create a soft link:
ln -s xmlshow.sh xmlshow
- 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
- On Debian,
apt get install
- On MacOs,
brew install
is your friend (see instructions to install brew)
- On Debian,
Voilà, you should be all set.
The less utility has actually quite a few tricks up its sleeves.
Beside the usual commands, remember that:
z
jumps one window forwardw
jumps one window backwardh
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
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!
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.