<p>I use emacs to edit my xml files (nxml-mode) and the files were generated by machine don't have any pretty formatting of the tags. </p>

<p>I have searched for pretty printing the entire file with indentation and saving it, but wasn't able to find an automatic way.</p>

<p>Is there a way? Or atleast some editor on linux which can do it. </p>

[{'answer_id': 12534, 'author': 'Marcel Levy', 'author_id': 676, 'author_profile': 'https://Stackoverflow.com/users/676', 'pm_score': 6, 'selected': True, 'text': '<p>I use <a href="http://www.thaiopensource.com/nxml-mode/" rel="noreferrer">nXML mode</a> for editing and <a href="http://tidy.sourceforge.net/" rel="noreferrer">Tidy</a> when I want to format and indent XML or HTML. There is also <a href="http://www.emacswiki.org/emacs/tidy.el" rel="noreferrer">an Emacs interface to Tidy.</a></p>\n'}, {'answer_id': 12564, 'author': 'cnu', 'author_id': 1448, 'author_profile': 'https://Stackoverflow.com/users/1448', 'pm_score': 1, 'selected': False, 'text': '<p>Tidy looks like a good mode. Must look at it. Will use it if I really need all the features it offers. </p>\n\n<p>Anyway, this problem was nagging me for about a week and I wasn\'t searching properly. After posting, I started searching and found one site with an <a href="http://blog.bookworm.at/2007/03/pretty-print-xml-with-emacs.html" rel="nofollow noreferrer">elisp function</a> which does it pretty good. The author also suggests using Tidy.</p>\n\n<p>Thanks for answer Marcel <strike>(too bad I don\'t have enough points to upmod you)</strike>. </p>\n\n<p><strike>Will post about it soon on my blog.</strike> Here is a <a href="http://codelog.blogial.com/2008/08/15/pretty-print-xml-files-in-emacs/" rel="nofollow noreferrer">post about it</a> (with a link to Marcel\'s site).</p>\n'}, {'answer_id': 58490, 'author': 'DaveP', 'author_id': 3577, 'author_profile': 'https://Stackoverflow.com/users/3577', 'pm_score': 2, 'selected': False, 'text': "<ol>\n<li>Emacs nxml-mode can work on presented format, but you'll have to split the lines.</li>\n<li>For longer files that simply isn't worth it. Run this stylesheet (ideally with Saxon\nwhich IMHO gets the line indents about right) against longer files\nto get a nice pretty print. For any elements where you want to retain white space\nadd their names alongside 'programlisting' as in 'programlisting yourElementName'</li>\n</ol>\n\n<p>HTH</p>\n\n<p>\n\n  \n   \n  </p>\n\n<p>\n   \n   \n   \n   \n </p>\n\n<p></p>\n"}, {'answer_id': 570049, 'author': 'Christian Berg', 'author_id': 5035, 'author_profile': 'https://Stackoverflow.com/users/5035', 'pm_score': 6, 'selected': False, 'text': '<p>If you only need pretty indenting without introducing any new line-breaks, you can apply the <code>indent-region</code> command to the entire buffer with these keystrokes:</p>\n<pre><code>C-x h\nC-M-\\\n</code></pre>\n<p>If you also need to introduce line-breaks, so that opening and closing tags are on separate lines, you could use the following very nice elisp function, written by <a href="http://blog.bookworm.at/2007/03/pretty-print-xml-with-emacs.html" rel="nofollow noreferrer">Benjamin Ferrari</a>. I found it on his blog and hope it\'s ok for me to reproduce it here:</p>\n<pre><code>(defun bf-pretty-print-xml-region (begin end)\n  &quot;Pretty format XML markup in region. You need to have nxml-mode\nhttp://www.emacswiki.org/cgi-bin/wiki/NxmlMode installed to do\nthis.  The function inserts linebreaks to separate tags that have\nnothing but whitespace between them.  It then indents the markup\nby using nxml\'s indentation rules.&quot;\n  (interactive &quot;r&quot;)\n  (save-excursion\n    (nxml-mode)\n    (goto-char begin)\n    (while (search-forward-regexp &quot;\\&gt;[ \\\\t]*\\&lt;&quot; nil t) \n      (backward-char) (insert &quot;\\n&quot;) (setq end (1+ end)))\n    (indent-region begin end))\n  (message &quot;Ah, much better!&quot;))\n</code></pre>\n<p>This doesn\'t rely on an external tool like Tidy.</p>\n'}, {'answer_id': 595572, 'author': 'Juan Garcia', 'author_id': 71942, 'author_profile': 'https://Stackoverflow.com/users/71942', 'pm_score': 7, 'selected': False, 'text': '<p>You don\'t even need to write your own function - sgml-mode (a gnu emacs core module) has a built-in pretty printing function called (sgml-pretty-print ...) which takes region beginning and end arguments.</p>\n\n<p>If you are cutting and pasting xml and you find your terminal is chopping the lines in arbitrary places you can use this <a href="http://curiousprogrammer.wordpress.com/2009/02/27/pretty-printing-xml/" rel="noreferrer">pretty printer</a> which fixes broken lines first.</p>\n'}, {'answer_id': 854101, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>I'm afraid I like Benjamin Ferrari version much better. The internal pretty print always places the end tag in a new line after the value, inserting unwanted CR in the tag values.</p>\n"}, {'answer_id': 2181771, 'author': 'Tim Helmstedt', 'author_id': 264043, 'author_profile': 'https://Stackoverflow.com/users/264043', 'pm_score': 5, 'selected': False, 'text': '<p>Emacs can run arbitrary commands with M-|.  If you have xmllint installed:</p>\n\n<p>"M-| xmllint --format -" will format the selected region</p>\n\n<p>"C-u M-| xmllint --format -" will do the same, replacing the region with the output</p>\n'}, {'answer_id': 4280824, 'author': 'bubak', 'author_id': 520637, 'author_profile': 'https://Stackoverflow.com/users/520637', 'pm_score': 4, 'selected': False, 'text': '<p>Thanks to Tim Helmstedt above I made st like this:</p>\n\n<pre><code>(defun nxml-pretty-format ()\n    (interactive)\n    (save-excursion\n        (shell-command-on-region (point-min) (point-max) "xmllint --format -" (buffer-name) t)\n        (nxml-mode)\n        (indent-region begin end)))\n</code></pre>\n\n<p>fast and easy.  Many thanks.</p>\n'}, {'answer_id': 5198243, 'author': 'Jason Viers', 'author_id': 480667, 'author_profile': 'https://Stackoverflow.com/users/480667', 'pm_score': 3, 'selected': False, 'text': '<p>here\'s a few tweaks I made to Benjamin Ferrari\'s version:</p>\n\n<ul>\n<li>the <code>search-forward-regexp</code> didn\'t specify an end, so it would operate on stuff from beginning of region to end of buffer (instead of end of region)</li>\n<li>Now increments <code>end</code> properly, as Cheeso noted.</li>\n<li>it would insert a break between <code>&lt;tag&gt;&lt;/tag&gt;</code>, which modifies its value.  Yes, technically we\'re modifying values of everything here, but an empty start/end is much more likely to be significant.  Now uses two separate, slightly more strict searches to avoid that.</li>\n</ul>\n\n<p>Still has the "doesn\'t rely on external tidy", etc. However, it does require <code>cl</code> for the <code>incf</code> macro.</p>\n\n<pre><code>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n;; pretty print xml region\n(defun pretty-print-xml-region (begin end)\n  "Pretty format XML markup in region. You need to have nxml-mode\nhttp://www.emacswiki.org/cgi-bin/wiki/NxmlMode installed to do\nthis.  The function inserts linebreaks to separate tags that have\nnothing but whitespace between them.  It then indents the markup\nby using nxml\'s indentation rules."\n  (interactive "r")\n  (save-excursion\n    (nxml-mode)\n    (goto-char begin)\n    ;; split &lt;foo&gt;&lt;foo&gt; or &lt;/foo&gt;&lt;foo&gt;, but not &lt;foo&gt;&lt;/foo&gt;\n    (while (search-forward-regexp "&gt;[ \\t]*&lt;[^/]" end t)\n      (backward-char 2) (insert "\\n") (incf end))\n    ;; split &lt;foo/&gt;&lt;/foo&gt; and &lt;/foo&gt;&lt;/foo&gt;\n    (goto-char begin)\n    (while (search-forward-regexp "&lt;.*?/.*?&gt;[ \\t]*&lt;" end t)\n      (backward-char) (insert "\\n") (incf end))\n    (indent-region begin end nil)\n    (normal-mode))\n  (message "All indented!"))\n</code></pre>\n'}, {'answer_id': 8004288, 'author': 'rajashekar', 'author_id': 1028948, 'author_profile': 'https://Stackoverflow.com/users/1028948', 'pm_score': 3, 'selected': False, 'text': '<p>One way of doing is \nIf you have something in below format</p>\n\n<pre><code>&lt;abc&gt;     &lt;abc&gt;&lt;abc&gt;   &lt;abc&gt;&lt;/abc&gt; &lt;/abc&gt;&lt;/abc&gt;       &lt;/abc&gt;\n</code></pre>\n\n<p>In Emacs, try</p>\n\n<pre><code>M-x nxml-mode\nM-x replace-regexp RET  &gt; *&lt; RET &gt;C-q C-j&lt; RET \nC-M-\\ to indent\n</code></pre>\n\n<p>This will indent above xml example to below</p>\n\n<pre><code>&lt;abc&gt;\n  &lt;abc&gt;\n    &lt;abc&gt;\n      &lt;abc&gt;\n      &lt;/abc&gt;\n    &lt;/abc&gt;\n  &lt;/abc&gt;\n&lt;/abc&gt;\n</code></pre>\n\n<p>In VIM you can do this by</p>\n\n<pre><code>:set ft=xml\n:%s/&gt;\\s*&lt;/&gt;\\r&lt;/g\nggVG=\n</code></pre>\n\n<p>Hope this helps.</p>\n'}, {'answer_id': 14589619, 'author': 'Cheeso', 'author_id': 48082, 'author_profile': 'https://Stackoverflow.com/users/48082', 'pm_score': 2, 'selected': False, 'text': '<p>I took <a href="https://stackoverflow.com/a/5198243/48082">Jason Viers\' version</a> and added logic to put xmlns declarations on their own lines. This assumes that you have xmlns= and xmlns: with no intervening whitespace.</p>\n\n<pre><code>(defun cheeso-pretty-print-xml-region (begin end)\n  "Pretty format XML markup in region. You need to have nxml-mode\nhttp://www.emacswiki.org/cgi-bin/wiki/NxmlMode installed to do\nthis.  The function inserts linebreaks to separate tags that have\nnothing but whitespace between them.  It then indents the markup\nby using nxml\'s indentation rules."\n  (interactive "r")\n  (save-excursion\n    (nxml-mode)\n    ;; split &lt;foo&gt;&lt;bar&gt; or &lt;/foo&gt;&lt;bar&gt;, but not &lt;foo&gt;&lt;/foo&gt;\n    (goto-char begin)\n    (while (search-forward-regexp "&gt;[ \\t]*&lt;[^/]" end t)\n      (backward-char 2) (insert "\\n") (incf end))\n    ;; split &lt;foo/&gt;&lt;/foo&gt; and &lt;/foo&gt;&lt;/foo&gt;\n    (goto-char begin)\n    (while (search-forward-regexp "&lt;.*?/.*?&gt;[ \\t]*&lt;" end t)\n      (backward-char) (insert "\\n") (incf end))\n    ;; put xml namespace decls on newline\n    (goto-char begin)\n    (while (search-forward-regexp "\\\\(&lt;\\\\([a-zA-Z][-:A-Za-z0-9]*\\\\)\\\\|[\'\\"]\\\\) \\\\(xmlns[=:]\\\\)" end t)\n      (goto-char (match-end 0))\n      (backward-char 6) (insert "\\n") (incf end))\n    (indent-region begin end nil)\n    (normal-mode))\n  (message "All indented!"))\n</code></pre>\n'}, {'answer_id': 21511772, 'author': 'Jarekczek', 'author_id': 772981, 'author_profile': 'https://Stackoverflow.com/users/772981', 'pm_score': 1, 'selected': False, 'text': '<p>I use <strong><code>xml-reformat-tags</code></strong> from <strong><a href="http://www.emacswiki.org/emacs/xml-parse.el" rel="nofollow">xml-parse.el</a></strong>. Usually you will want to have the point at the beginning of the file when running this command.</p>\n\n<p>It\'s interesting that the file is incorporated into <a href="http://emacspeak.sourceforge.net/" rel="nofollow">Emacspeak</a>. When I was using Emacspeak on day-by-day basis, I thought <code>xml-reformat-tags</code> is an Emacs builtin. One day I lost it and had to make an internet search for that, and thus entered the wiki page mentioned above.</p>\n\n<p>I\'m attaching also my code to start xml-parse. Not sure if this is the best piece of Emacs code, but seems to work for me.</p>\n\n<pre><code>(if (file-exists-p "~/.emacs.d/packages/xml-parse.el")\n  (let ((load-path load-path))\n    (add-to-list \'load-path "~/.emacs.d/packages")\n    (require \'xml-parse))\n)\n</code></pre>\n'}, {'answer_id': 37539920, 'author': 'Talespin_Kit', 'author_id': 579689, 'author_profile': 'https://Stackoverflow.com/users/579689', 'pm_score': 5, 'selected': False, 'text': '<p>For introducing line breaks and then pretty printing</p>\n\n<pre><code>M-x sgml-mode\nM-x sgml-pretty-print\n</code></pre>\n'}, {'answer_id': 38868126, 'author': 'JohnnyZ', 'author_id': 1273287, 'author_profile': 'https://Stackoverflow.com/users/1273287', 'pm_score': 1, 'selected': False, 'text': '<p>If you use <a href="http://spacemacs.org" rel="nofollow">spacemacs</a>, just use command \'spacemacs/indent-region-or-buffer\'. </p>\n\n<pre><code>M-x spacemacs/indent-region-or-buffer\n</code></pre>\n'}, {'answer_id': 43269076, 'author': 'ninrod', 'author_id': 4921402, 'author_profile': 'https://Stackoverflow.com/users/4921402', 'pm_score': 2, 'selected': False, 'text': '<p>as of 2017 emacs already comes with this capability by default, but you have to write this little function into your <code>~/.emacs.d/init.el</code>:</p>\n\n<pre><code>(require \'sgml-mode)\n\n(defun reformat-xml ()\n  (interactive)\n  (save-excursion\n    (sgml-pretty-print (point-min) (point-max))\n    (indent-region (point-min) (point-max))))\n</code></pre>\n\n<p>then just call <code>M-x reformat-xml</code></p>\n\n<p>source: <a href="https://davidcapello.com/blog/emacs/reformat-xml-on-emacs/" rel="nofollow noreferrer">https://davidcapello.com/blog/emacs/reformat-xml-on-emacs/</a></p>\n'}]
['https://Stackoverflow.com/questions/12492', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1448/']