Related: How can I pretty-print JSON in (unix) shell script?

Is there a (unix) shell script to format XML in human-readable form?

Basically, I want it to transform the following:

<root><foo a="b">lorem</foo><bar value="ipsum" /></root>

... into something like this:

<root>
    <foo a="b">lorem</foo>
    <bar value="ipsum" />
</root>
share|improve this question
up vote 588 down vote accepted

Try doing this :

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
    xmllint --format -

This utility comes with libxml2-utils

or

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
    xml_pp

this command comes with XML::Twig module, sometimes xml-twig-tools package.

or

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
    xmlstarlet format --indent-tab

this command comes with xmlstarlet

or

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
    tidy -xml -i -

check tidy package

or

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
    python -c 'import sys;import xml.dom.minidom;s=sys.stdin.read();print xml.dom.minidom.parseString(s).toprettyxml()'

or

echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' |
    saxon-lint --indent --xpath '/' -  

check saxon-lint

or

 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'

Check saxon-HE

share|improve this answer
    
Good, quick answer. The first option seems like it'll be more ubiquitous on modern *nix installs. A minor point; but can it be called without working through an intermediate file? I.e., echo '<xml .. />' | xmllint --some-read-from-stdn-option? – svidgen Apr 18 '13 at 19:08
    
See my edited post – Gilles Quenot Apr 18 '13 at 19:19
    
Added xmlstarlet solution – Gilles Quenot Nov 5 '13 at 21:24
    
The package is libxml2-utils in my beautiful ubuntu. – franzlorenzon Feb 7 '14 at 9:23
1  
Note that the "cat data.xml | xmllint --format - | tee data.xml" does not work. On my system it sometimes worked for small files, but always truncated huge files. If you really want to do anything in place read backreference.org/2011/01/29/in-place-editing-of-files – user1346466 Dec 3 '14 at 18:55

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:

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

MacOS

To install this on MacOS with Homebrew just do: brew install libxml2

share|improve this answer
4  
sputnick's answer contains this information, but crmpicco's answer is the most useful answer here to the general question about how to pretty print XML. – Seth Difley Nov 26 '14 at 18:08
2  
we can write out that formatted xml output to some other xml file and use that.. eg xmllint --format yourxmlfile.xml >> new-file.xml – LearnToLive Jan 13 '16 at 15:53
2  
On Ubuntu 16.04 you can use the following: sudo apt-get install libxml2-utils – Melle Jan 24 '17 at 9:53
    
This works on Windows too; git for Windows download even installs a recent version of xmllint. Example: "C:\Program Files\Git\usr\bin\xmllint.exe" --format QCScaper.test@borland.com.cds.xml > QCScaper.test@borland.com.pretty-printed.cds.xml – Jeroen Wiert Pluimers Dec 21 '17 at 7:46

You can also use tidy, which may need to be installed first (e.g. on Ubuntu: sudo apt-get install tidy).

For this, you would issue something like following:

tidy -xml -i your-file.xml > output.xml

Note: has many additional readability flags, but word-wrap behavior is a bit annoying to untangle (http://tidy.sourceforge.net/docs/quickref.html).

share|improve this answer
    
Helpful, because I couldn't get xmllint to add linebreaks to a single line xml file. Thanks! – xlttj Nov 12 '14 at 16:00
    
tidy works well for me too. Unlike hxnormalize, this done actually closes the <body> tag. – Sridhar-Sarnobat Nov 25 '14 at 23:07
5  
BTW, here are some options that I have found useful: tidy --indent yes --indent-spaces 4 --indent-attributes yes --wrap-attributes yes --input-xml yes --output-xml yes < InFile.xml > OutFile.xml. – Victor Yarema Feb 19 '16 at 10:02
    
Great tip @VictorYarema. I combined it with pygmentize and added it to my .bashrc: alias prettyxml='tidy --indent yes --indent-spaces 4 --indent-attributes yes --wrap-attributes yes --input-xml yes --output-xml yes | pygmentize -l xml' and then can curl url | prettyxml – Net Wolf Nov 12 '17 at 23:45

To have xmllint available on Debian systems, you need to install the package libxml2-utils (libxml2 does not provide this tool, at least not on Debian 5.0 "Lenny" and 6.0 "Squeeze").

share|improve this answer

You didn't mention a file, so I assume you want to provide the XML string as standard input on the command line. In that case, do the following:

$ echo '<root><foo a="b">lorem</foo><bar value="ipsum" /></root>' | xmllint --format -
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.