9

I know of two ways to "pretty print", or format, xml:

  1. shell tools
  2. Hack 38 Pretty-Print XML Using a Generic Identity Stylesheet and Xalan

what other free (as in beer) formatters are there? (aside from using javascript)

hoijui
  • 3,615
  • 2
  • 33
  • 41
Thufir
  • 8,216
  • 28
  • 125
  • 273
  • Just for completeness and better discoverability I link to [pretty printing xml with javascript](https://stackoverflow.com/questions/376373/pretty-printing-xml-with-javascript) (which is probably the canonical SO thread regarding javascript solutions to this problem) – Martin Jul 27 '21 at 11:00

5 Answers5

15

Well, the identity transform you linked to is portable to any XSLT processor (Saxon, msxml, etc).

Additionally, you could look at xmllint which is part of the LibXML2 toolkit. The --format option allows you to pretty print the input. Similar functionality exists in XMLStarlet (which uses LibXML2 under the hood iirc).

Nic Gibson
  • 7,051
  • 4
  • 31
  • 40
10

xmlstarlet fo is what I use for pretty printing. Xmlstarlet has a number of options:

$ xmlstarlet 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

A good XML engineer should be able to wield xmlstarlet.

hendry
  • 9,725
  • 18
  • 81
  • 139
3

You can use http://prettydiff.com/?m=beautify Unfortunately, it is written in JavaScript, but it is a complete application so you never have to know that. Just know that you can run from inside your browser without downloading or installing anything.

austincheney
  • 1,097
  • 7
  • 8
1

I like the java library XOM for XML manipulation. It has a nice Pretty Printer that provides a lot of control over the output.

hoijui
  • 3,615
  • 2
  • 33
  • 41
orangepips
  • 9,891
  • 6
  • 33
  • 57
0

When using libxml2 in python:

with open(pathToSaveResult, 'w') as fd:
   xmlParsed.saveTo(fd,format = libxml2.XML_SAVE_FORMAT)

Edit: It looks like there is some bug in libxml2 ...pretty printing is done with the tag libxml2.XML_SAVE_NO_EMPTY instead of libxml2.XML_SAVE_FORMAT

Alex
  • 1,602
  • 20
  • 33