shell

Adding formatting to an XML document

Sometimes, when I have to program a web service client I have to deal with unformatted XML files. For example, the next one:

<users><user><email>pfortuny@commandliners.com</email><passwd>a0f901492d89fe2ba88cc96bf9d
2475e</passwd></user><user><email>n0str0m0@commandliners.com</email><passwd>7e1b6dbfa824d
5d114e96981cededd00</passwd></user><user><email>rafacas@commandliners.com</email><passwd>
70c1db56f301c9e337b0099bd4174b28</passwd></user></users>

This is not a bad thing, because it is sent that way to save traffic, but I’d rather see it in a human readable format. So I use the xmllint command, that reformat and reindent the input.

$ xmllint --format test.xml
<?xml version="1.0"?>
<users>
  <user>
    <email>pfortuny@commandliners.com</email>
    <passwd>a0f901492d89fe2ba88cc96bf9d2475e</passwd>
  </user>
  <user>
    <email>n0str0m0@commandliners.com</email>
    <passwd>7e1b6dbfa824d5d114e96981cededd00</passwd>
  </user>
  <user>
    <email>rafacas@commandliners.com</email>
    <passwd>70c1db56f301c9e337b0099bd4174b28</passwd>
  </user>
</users> 

The indentation can be controlled by the environment variable XMLLINT_INDENT. The default value is two spaces.

pfortuny, n0str0m0, do not worry guys, those are not your passwords, are they? ;)

speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*Required Fields