The other day I found myself struggling to decipher a stream of SOAP requests and responses from one of our server logs.
The offending XML was all mashed together without any line-breaks (cue headache).
To make it easier for me to debug the data contained in those SOAP messages I wanted Emacs to “pretty-print” a block of XML for me.
I added a new Emacs Lisp function to accomplish this by customising my
~/.emacs file with the following snippet of code:
(defun xml-format () (interactive) (save-excursion (shell-command-on-region (mark) (point) "xmllint --format -" (buffer-name) t) ) )
xmllint is a unix command line tool used to parse XML files.
xml-format passes the selected region of an Emacs buffer as standard input to xmllint and then replaces that region with the new formatted XML.
I can now select a region of XML in an Emacs buffer, execute the function xml-format ( M-x , xml_format , RET ) and …
Abracadabra, each XML element is displayed on its own line and as an extra bonus the region has been nicely indented to boot.
<?xml version="1.0"?><book><title>Tcl/Tk: A Developer's Guide</title><author>Clif Flynt</author><publisher>Morgan Kauffman</publisher></book>
<?xml version="1.0"?> <book> <title>Tcl/Tk: A Developer's Guide</title> <author>Clif Flynt</author> <publisher>Morgan Kauffman</publisher> </book>
Much Easier to read :-)