MW.
MW.

Reputation: 199

Is there a way to use TextWrangler to clean-up / tidy messy XML?

I don't need it to validate ( though that would be nice I don't think text wrangler does this) but to clean up messy .xml.

example this ...

<some><foo>
bar</foo></some>

to ...

<some>
   <foo>bar</foo>
</some>

thanks -MW

Upvotes: 9

Views: 21281

Answers (5)

ecomware
ecomware

Reputation: 1

If you're on a Mac, it may be easiest to create the script this way:

#!/bin/bash
pbpaste | xmllint --c14n - | XMLLINT_INDENT=$'\t' xmllint --encode UTF-8 --format - | pbcopy

Right-click the file, click Get Info, and change "Open With" to terminal. This will let you process xml in the primary clipboard from anywhere by clicking the icon. ie. copy-click-paste. You can also wrap with another so that it's accessible both ways.

pbpaste | ./tidy.sh | pbcopy # where tidy.sh is available to TextWrangler

Upvotes: 0

Dennis Ng
Dennis Ng

Reputation: 79

The script does not work still under 4.5.

I checked https://groups.google.com/forum/?fromgroups=#!topic/textwrangler/FePYfNKi4rs and it had a script that worked.

#!/bin/sh 

XMLLINT_INDENT=$'\t' xmllint --format --encode utf-8 -

Upvotes: 2

nwinkler
nwinkler

Reputation: 54467

As an update to the instructions from http://magp.ie/2010/02/15/format-xml-with-textwrangler/ and the comment by @Cykoduck for getting this to work in TextWrangler version 4.

The script needs to be changed to take the input from STDIN instead of a temporary file, so the first invocation of xmllint needs to be changed:

#!/bin/sh
xmllint --c14n - | XMLLINT_INDENT=$'\t' xmllint --encode UTF-8 --format -

This way it works for TextWrangler 4 as well. The menu item to call the script has been moved to the Text menu in this version.

Reference link: https://groups.google.com/forum/?fromgroups#!topic/textwrangler/FePYfNKi4rs

Upvotes: 3

Cykoduck
Cykoduck

Reputation: 121

I also used the method on http://magp.ie/2010/02/15/format-xml-with-textwrangler/

but I modified it because the errors I was getting regarding the xml I was attempting to format. My script is just:

#!/bin/sh
xmllint "$*" | XMLLINT_INDENT=$'\t' xmllint --encode UTF-8 --format -

I took out the formatting for the W3C canonical format to fix my errors like yours.

Upvotes: 2

Reddog
Reddog

Reputation: 15579

If you're after a tool, most editors have some sort of "Tidy" feature.

  • In NotePad++: TestFX -> TestFX HTML Tidy -> Tidy: Reindent XML
  • In Visual Studio: Ctrl-K, Ctrl-D (or Edit -> Advanced -> Format Document)

A quick google for TextWrangler turns up this - http://magp.ie/2010/02/15/format-xml-with-textwrangler/

Upvotes: 6

Related Questions