user2142786
user2142786

Reputation: 1482

How to validate HTML using JTidy?

I want to validate my HTML using JTidy. I want a response of true or false if it is valid or not, respectively. Currently I am using this code:

String htmlData = "<html><head></head><body><div>Hello Java </div></body></html>";  
Tidy tidy = new Tidy();

InputStream stream = new ByteArrayInputStream(htmlData.getBytes());
tidy.parse(stream, System.out);

Is there a way to get a boolean response whether my HTML is valid or not?

Upvotes: 2

Views: 3185

Answers (1)

Tariq
Tariq

Reputation: 2569

According to the documentation you can get total error counts for your last parse operation using getParseErrors

So you can do something like

private boolean isValid(String htmlData){
   Tidy tidy = new Tidy();
   InputStream stream = new ByteArrayInputStream(htmlData.getBytes());
   tidy.parse(stream, System.out);
   return (tidy.getParseErrors() == 0);
}

Upvotes: 2

Related Questions