3

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
User1
  • 39,458
  • 69
  • 187
  • 265

3 Answers3

6

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.

Community
  • 1
  • 1
Anthony
  • 36,459
  • 25
  • 97
  • 163
  • 1
    What could also be done is passing it through xmllint (part of the libxml2 package). – amphetamachine Feb 01 '10 at 23:14
  • Do you happen to know what notepad++ uses? I am pretty sure it is a library and not a simple function. – Anthony Feb 01 '10 at 23:19
  • Yes, "pretty print" was the term I needed to find that post. However, it took about half a minute for nXml mode to "parse" my doc before it even started the indenting and other stuff. Can I turn that off? xml-mode works much faster for me. – User1 Feb 01 '10 at 23:19
  • 1
    nXml used to really choke on single-line XML files; one of the reasons I stopped using it by default years ago. – Joe Casadonte Feb 02 '10 at 13:39
4

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)
)
kovan
  • 680
  • 1
  • 5
  • 15
2

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!

Joe Casadonte
  • 15,888
  • 11
  • 45
  • 57