Format XML with TextWrangler

My text editor of choice on the Mac is TextWrangler. It’s lightweight and it has pretty much all you need from a text editor. In particular, I like that I can SFTP into my development server.

One issue that bugged me lately was when I opened an unindented, unformatted XML file. Basically, it looked a mess and there was no way to tidy the file up so that I could read it easily.

However, I found a simple way to do this today… thanks to this and this.

Simple guide
We want to add a UNIX script to TextWrangler so it can format an XML file… to do this, do this…

  • Open TextWrangler and open a new text file.
  • Copy and paste the code below into this file.
  • #!/bin/sh
    xmllint --c14n "$*" | XMLLINT_INDENT=$'\t' xmllint --encode UTF-8 --format -
    

  • Save the file, something like Tidy XML.sh, in the Filters folder.
  • You can find the filters folder by going to the #! menu and go to the Unix Filters sub-menu, and select the Open Filters Folder, like below.

    • Now anytime you want to format an XML file, just go to this menu and select the Tidy XML.sh script and BOOM, neat tidy XML.

This is an interesting facility to extend an already great text editor, and I will be looking into more cool scripts that can hopefully lessen my daily annoyances.

UPDATED:: Added UTF8 encoding, thanks Rolan.
UPDATED:: Added a post to format PHP code in TextWrangler.

29 thoughts on “Format XML with TextWrangler

  1. Pingback: Format XML with TextWrangler – Cristian’s blog

  2. Pingback: Textwrangler Tips – Compare files, keyboard shortcuts « Magp.ie

  3. Thanks!
    Small addition that was useful for me:
    If you are working with UTF-8 encoded files – then the following parameter is required:

    --encode UTF-8

  4. Pingback: Tidy and format your PHP and meet WordPress standards on Coda and TextWrangler « Magp.ie

  5. great tips.. I have been trying to find how to implement this in TextWrangler from many sites, but yours is the easiest to understand ..

    cheers!

  6. Hi,
    Great script. I have had only one problem. It’s not formating properly empty tags like that:

    So for a file it looks like that:

    6982760

    graphic
    89.000000

    A20110609T092928_M_192_168_112_212_01.eps

    Any chance to fix it?
    Piotr

  7. Great solution!

    However, there’s a big caveat: Tidy also deletes CDATA tags! If you need your XML to maintain the original text as it was, you may run into trouble.

    Tidy does “the right thing” by replacing sensitive characters with their entities, e.g.:

    <value>[CDATA[Your measurement of n was correct if n<2.5]]></value>

    is transformed to

    <value>Your measurement of n was correct if n&lt;2.5</value>

    This is OK for information in an HTML context, but not if you need the content for other output channels like print!

  8. Dude, where can I send $1,000,000.00? Seriously, Text Wrangler is my text editor of choice but it’s lack of pretty printing functionality for xml was a pain. I have been looking for a good xml editor to accomplish just what this simple script does. Thanks a bunch!

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s