XML reindent

XML is designed to be human-readable. However, to minimise data transfer, the indentation and newline characters are often discarded during transmission. This results in a difficult-to-read XML file such as the one below:

<?xml version="1.0" encoding="ISO-8859-1"?><note><to>Tove</to><from>Jani</from>
<heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

This XML file can be both checked for validity, and reindented using xmllint. Debian/Ubuntu users can find this as part of the libxml2-utils package.

xmllint --format input.xml > output.xml

This results in the following, more-sane output:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

Leave a Reply