Building simplicidade.org: notes, projects, and occasional rants

Pretty print XML

In case you didn't know, you can pretty print XML with the xmllint command line tool that comes with libxml2, and its installed by default with Mac OS X.

Basic usage is

xmllint --format xml_file.xml

and the pretty version will be sent to standard output.

But working with XMPP and SAPO Broker, I'm always copy&pasting XML from one place to the other and it would be nice to format the XML snippet sitting in the clipboard.

This pipe does the trick quite nicely:

pbpaste | xmllint --format - | pbcopy

I wrapped this into a script, called x-xml-format-clipboard and now its just one command away from gratification.

Next step, create a version that I can place in the services menu. I need to find a way to access the selected text on the current Cocoa app. I hope thats not to hard. Making the final script a service is definitively easy, using the ThisService app.