tags:

views:

145

answers:

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.

+5  A: 

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.

Anthony
What could also be done is passing it through xmllint (part of the libxml2 package).
amphetamachine
Do you happen to know what notepad++ uses? I am pretty sure it is a library and not a simple function.
Anthony
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
nXml used to really choke on single-line XML files; one of the reasons I stopped using it by default years ago.
Joe Casadonte
+1  A: 

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
+1  A: 

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
Brilliant! Way faster than reformatting with nxml.
Chopmo