Pretty-printing XML with Emacs’ NXML-mode

Did you ever get a stream of XML out of a log file, or in a data stream, and it’s all mashed together without line-breaks so that it just appears as gobble-de-gook? If there’s a data error (not an XML parsing error) then you have to read it so that you can find where the error is, but you don’t have XML-spy and NetBeans is overkill or takes forever to fire up…

Emacs to the rescue! Benjamin Ferrari wrote this increadibly useful (and simple) elisp function to pretty-print a block of XML code:

(defun bf-pretty-print-xml-region (begin end)
  "Pretty format XML markup in region. You need to have nxml-mode
http://www.emacswiki.org/cgi-bin/wiki/NxmlMode installed to do
this.  The function inserts linebreaks to separate tags that have
nothing but whitespace between them.  It then indents the markup
by using nxml's indentation rules."
  (interactive "r")
  (save-excursion
      (nxml-mode)
      (goto-char begin)
      (while (search-forward-regexp "\>[ \\t]*\<" nil t)
        (backward-char) (insert "\n"))
      (indent-region begin end))
    (message "Ah, much better!"))

Indeed, why isn’t this included with NXML-mode?Β  Ah well, at least my problem’s solved for the simple use-case of opening a machine-generated XML file and wanting to visually parse it…

Benjamin’s blog has lots of useful Emacs stuff. Worth a read.

5 thoughts on “Pretty-printing XML with Emacs’ NXML-mode

    • You could use sed:

      sed s/[\ \t\n]//g < somefile.xml > newfile.xml

      Or, one way to do the same in emacs would be with M-x replace-regexp. For the regexp, you just type TAB for the TAB char, and C-q C-j for the newline. It’ll look like this on the minibuffer:

      Replace regexp: [
      ]
      


      Emacs reminds you if you type \t or \n though πŸ™‚

Comments are closed.