Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

format xml, pretty print

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)

like image 801
Thufir Avatar asked Nov 09 '10 00:11

Thufir


People also ask

How do I Print a pretty XML file?

The xml format command has four options to control the output: -n or –noindent: do not indent the output. -t or –indent-tab: indent output with TABs. -s or –indent-spaces <num>: indent output with <num> spaces.

How do I beautify XML in Notepad?

Then there are a couple keyboard shortcuts to beautify the XML: Pretty Print: Ctrl + Shift + Alt + B. Pretty Print (indent attributes): Ctrl + Shift + Alt + A.

How do I beautify XML in Intellij?

Either open your file in the editor and press Ctrl+Alt+Shift+L or in the Project tool window, right-click the file and select Reformat Code.


2 Answers

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).

like image 158
Nic Gibson Avatar answered Oct 06 '22 08:10

Nic Gibson


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.

like image 29
hendry Avatar answered Oct 06 '22 07:10

hendry