How to

You can use JTidy as an html checker/prettyprinter or as a DOM parser.

First of all, you will need to download a JTidy distribution. Inside it you will find a jtidy-{version}.jar, where {version} is the JTidy release number: this is the jar containing all the JTidy classes you need, and we will name it this simply jtidy.jar in the following how to. No other libraries are needed.

Now that you have JTidy you can use it in different ways.

JTidy executable

Run java -jar jtidy.jar {options} to access JTidy command line interface.

java -jar jtidy.jar -h will output a short help on jtidy command line with a few examples.

java -jar jtidy.jar -help-config will output all the available configuration options and java -jar jtidy.jar -show-config the current (default) values.

Ant task

Detailed instructions on how to use the JTidy ant task can be found in JTidyTask javadocs.

JTidy API

The entry point for accessing JTidy functionalities is the org.w3c.tidy.Tidy class. This is a simple example of use:

                    
Tidy tidy = new Tidy(); // obtain a new Tidy instance
tidy.setXHTML(boolean xhtml); // set desired config options using tidy setters 
...                           // (equivalent to command line options)

tidy.parse(inputStream, System.out); // run tidy, providing an input and output stream

                

Using parseDOM(java.io.InputStream in, java.io.OutputStream out) instead of parse() you will also obtain a DOM document you can parse and print out later using pprint(org.w3c.dom.Document doc, java.io.OutputStream out) (note that the JTidy DOM implementation is not fully-featured, and many DOM methods are not supported).

Starting from release r8, JTidy also provide a MessageListener interface you can implement to be notified for warning and errors in your html code. For details on advanced uses refer to jtidy javadocs.