Wednesday, April 22, 2015

Format XML with Gedit and xmllint

A very simple technique to format (beautify) xml using xmllint and gedit.

Gedit allows you to create plugins (external tools) which can act upon the current document.

Make sure xmllint is installed on your system.

1) Open gedit

2) Edit->Preferences->Plugins:  Check the "External Tools" box and close.

3) Tools->Manage External Tools

4) Click on the "new" button which looks like a plus in the bottom left corner of the window.

5) Insert a name for this plugin, "FormatXML" for example, then press enter.

6) Insert the following code in the "edit" portion of the screen:

#!/bin/sh
xmllint --format /dev/stdin


7) Set the "Input" menu to "Current document" and "Output" menu to "Replace current document".  Close the "manage external tools" window.

Now, you can paste or type unformatted XML.

And simply click on Tools->External Tools->FormatXML to format it properly.

1 comment:

  1. Briliant -- I had this function on my old computer, but have struggled to remember the library I used with Gedit! (The only thing I'm adding is the keyboard shortcut to ALT+X to trigger the function.)
    Thanks a lot! :-)

    ReplyDelete