User1
User1

Reputation: 41173

Formatting XML in Emacs

I have a project with huge XML files that I'm copying and pasting into Emacs to edit. It's all on a single line, so I'd like to have a tool to make one XML element per line. Is there an Emacs function that I can use? I guess I'll even settle for a command-line tool that nicely integrates with Emacs, but that's not ideal.

Upvotes: 3

Views: 1574

Answers (3)

kovan
kovan

Reputation: 770

I wrote a little Elisp function for that, that relies on xmllint from libxml:

(defun format-xml ()
  (interactive)
  (shell-command-on-region 1 (point-max) "xmllint --format -" (current-buffer) t)
)

Upvotes: 4

Joe Casadonte
Joe Casadonte

Reputation: 16859

I've used xml-parse for years to reformat XML. The specific command you want in that package is xml-reformat-tags. Hope that helps!

Upvotes: 2

Anthony
Anthony

Reputation: 37065

The feature you are looking for is typically called "pretty print". There is a pretty-print function for emacs at:

http://sinewalker.wordpress.com/2008/06/26/pretty-printing-xml-with-emacs-nxml-mode/

Also, take a look at this SO question which has other options.

Upvotes: 6

Related Questions