How to format XML files in Sublime Text 2

Let's face it, currently, the most used formats for transactions and sending information among systems are JSON and XML. From my personal perspective JSON is a better option when we need to use REST services, because:

  1. JSON is a lighter format.
  2. Most of the times we consume REST services from client-side -JavaScript-, using AJAX calls.
  3. Old and modern browsers can parse and transform JSON objects into JavaScript object with almost no work from our side.

The problem

But... sometimes, we need to use XML format and many times we download the XML responses to our local environment to understand its structure, and here is the little problem, browsers don't format XML when we download a response. Let's see an example:

Imagine you have this response:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><response><status><timeStamp>1395932927084</timeStamp><error_code>0</error_code><error_description></error_description><error_message></error_message><api_version>1.0</api_version></status><data><content><id>id_of_a_post</id><name>Learning-jQuery</name><title>Learning jQuery</title><description></description><summary></summary><image>/path/to/image/image.jpg</image><image_size>143591</image_size><thumbnail>/path/to/image/thumbnail.jpg</thumbnail><published_on>1385355600000</published_on></content></data></response>


Sublime Text 2 has a really good plugin to format XML files with a single click! Here is the plugin: SublimeText Indent plugin.

This plugin also works for JSON format.

  1. Open your Package Control (if you have never used Package Control, please see this documentation)
  2. Type Indent
  3. Click on the link/option 
  4. Wait for installation's finalization
  5. You will have a new option in Selection menu

After click on Indent XML you will see something like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
			<title>Learning jQuery</title>

About the author

Enterprise Web Developer

Alex is an experienced technology professional. His background includes:

  • Web Technologies: Semantic Web (RDF, OWL), MVC Frameworks (CakePHP, CodeIgniter) Content Management Systems (Drupal, Joomla, WordPress), JavaScript libraries (jQuery, Mootools), CSS3, HTML5, mobile web (Sencha, jQuery mobile), Operating Systems, and,
  • Research: Complex Systems (Cellular Automata in specific), Artificial Intelligence, Computer Graphics and Computer Simulation to create computer models of living.