xmllint from libxml2
xmllint --format file.xml
(On Debian-based distributions, install the libxml2-utils package.)
xml_pp from the XML::Twig perl module
xml_pp < file.xml
(On Debian-based distributions, install the xml-twig-tools package.)
XMLStarlet
xmlstarlet format --indent-tab file.xml
Tidy
tidy -xml -i -q file.xml
Python's xml.dom.minidom
echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
python -c 'import sys, xml.dom.minidom; print(xml.dom.minidom.parseString(sys.stdin.read()).toprettyxml())'
saxon-lint (my own project)
saxon-lint --indent --xpath '/' file.xml
saxon-HE
echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
java -cp /usr/share/java/saxon/saxon9he.jar net.sf.saxon.Query \
-s:- -qs:/ '!indent=yes'
xidel
xidel --output-node-format=xml --output-node-indent -se . -s file.xml
(Credit to Reino)
Output for all commands:
<root>
<foo a="b">lorem</foo>
<bar value="ipsum"/>
</root>
Answer from Gilles Quénot on Stack Overflowxmllint from libxml2
xmllint --format file.xml
(On Debian-based distributions, install the libxml2-utils package.)
xml_pp from the XML::Twig perl module
xml_pp < file.xml
(On Debian-based distributions, install the xml-twig-tools package.)
XMLStarlet
xmlstarlet format --indent-tab file.xml
Tidy
tidy -xml -i -q file.xml
Python's xml.dom.minidom
echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
python -c 'import sys, xml.dom.minidom; print(xml.dom.minidom.parseString(sys.stdin.read()).toprettyxml())'
saxon-lint (my own project)
saxon-lint --indent --xpath '/' file.xml
saxon-HE
echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
java -cp /usr/share/java/saxon/saxon9he.jar net.sf.saxon.Query \
-s:- -qs:/ '!indent=yes'
xidel
xidel --output-node-format=xml --output-node-indent -se . -s file.xml
(Credit to Reino)
Output for all commands:
<root>
<foo a="b">lorem</foo>
<bar value="ipsum"/>
</root>
xmllint --format yourxmlfile.xml
xmllint is a command line XML tool and is included in libxml2 (http://xmlsoft.org/).
================================================
Note: If you don't have libxml2 installed you can install it by doing the following:
CentOS
cd /tmp
wget ftp://xmlsoft.org/libxml2/libxml2-2.8.0.tar.gz
tar xzf libxml2-2.8.0.tar.gz
cd libxml2-2.8.0/
./configure
make
sudo make install
cd
Ubuntu
sudo apt-get install libxml2-utils
Cygwin
apt-cyg install libxml2
macOS
To install this on macOS with Homebrew just do:
brew install libxml2
Git
Also available on Git if you want the code:
git clone git://git.gnome.org/libxml2
formatting - How to prettify/format an XML buffer? - Emacs Stack Exchange
How to format XML document in Linux - Stack Overflow
an XML without LF want to make it pretty using sed command in shell - Unix & Linux Stack Exchange
Pretty printing XML?
Does the functionality already exist to reformat this buffer to be somewhat user-readable?
Of course, and you have plenty of options. I'd probably feed it to an external program using:
C-x h C-u M-| xmllint --format - RET
This program comes with libxml2. You could also use tidy. Here's a list of commandline xml formatting tools: https://stackoverflow.com/questions/16090869/how-to-pretty-print-xml-from-the-command-line
You could also do a search and replace and then indent:
M-% > < RET > C-q C-j < RET ! C-M-\
Neat trick: you can copy and paste the above string into M-: (eval-expression) like this:
(execute-kbd-macro (kbd "M-% > < RET > C-q C-j < RET ! C-M-\\"))
The built-in sgml-mode has a command to do this: sgml-pretty-print. If you're in nxml-mode it looks like you need switch to sgml-mode first. You could write a command to temporarily switch to sgml-mode, run pretty-print, then switch back to nxml-mode.
For example, here is a command that will pretty-print the region, optionally with auto-fill enabled:
(defun xml-pretty-print (beg end &optional arg)
"Reformat the region between BEG and END.
With optional ARG, also auto-fill."
(interactive "*r\nP")
(let ((fill (or (bound-and-true-p auto-fill-function) -1)))
(sgml-mode)
(when arg (auto-fill-mode))
(sgml-pretty-print beg end)
(nxml-mode)
(auto-fill-mode fill)))
xmllint -format -recover nonformatted.xml > formated.xml
For tab indentation:
export XMLLINT_INDENT=`echo -e '\t'`
For four space indentation:
export XMLLINT_INDENT=\ \ \ \
Without programming you can use Eclipse XML Source Editor. Have a look at this answer
By the way have you tried xmllint -format -recover nonformatted.xml > formated.xml?
EDIT:
You can try this XMLStarlet Command Line XML Toolkit.
5. Formatting XML documents
====================================================
xml fo --help
XMLStarlet Toolkit: Format XML document
Usage: xml fo [<options>] <xml-file>
where <options> are
-n or --noindent - do not indent
-t or --indent-tab - indent output with tabulation
-s or --indent-spaces <num> - indent output with <num> spaces
-o or --omit-decl - omit xml declaration <?xml version="1.0"?>
-R or --recover - try to recover what is parsable
-D or --dropdtd - remove the DOCTYPE of the input docs
-C or --nocdata - replace cdata section with text nodes
-N or --nsclean - remove redundant namespace declarations
-e or --encode <encoding> - output in the given encoding (utf-8, unicode...)
-H or --html - input is HTML
-h or --help - print help