Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Validating html tags in a string

 
Chigull Sujatha
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am looking to validate the html tags contained within a string object. Is there any validator/library available so I can just send the string and get a message if the tags are not valid. I have looked at w3cs html validator, but I needed to do something from within my code. Any suggestions. Pls. help!

Thanks,
Sujatha
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you generate the HTML you can make it well-formed XML at the same time. Google for "java xhtml validator" and see what you find.

I think the HTML spec says rendering engines should just ignore anything they don't recognize, so it's "valid" to add your own tags and attributes and such.
How "valid" do you want it to be?
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are hard pressed on detecting any invalid HTML tags then you can make use of HTMLEditorKit API. Take a look at javax.swing.text.html.* package.

This tutorial will help you in getting started on using these API's.
http://www.samspublishing.com/articles/article.asp?p=31059&rl=1

In short the core task will be to override HTMLEditorKit.ParserCallback class's handleError method. I know this question has been asked before in ranch if you search the site you must be able to get further information.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JTidy toolkit is the most highly developed HTML tool that I know of - it may be overkill for your problem.

Bill
 
Chigull Sujatha
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys, I will checkout the info you all suggested about.

Sujatha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic