aspose file tools*
The moose likes Servlets and the fly likes Encoding and Decoding HTML characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Encoding and Decoding HTML characters" Watch "Encoding and Decoding HTML characters" New topic
Author

Encoding and Decoding HTML characters

Kevin Weiss
Greenhorn

Joined: Mar 27, 2007
Posts: 7
Hi all,

I need to encode and decode HTML characters within a text String. For example, I need to convert:


to...

...and vice versa. Is there a class or package that handles these conversions?

PS - I'm NOT using the string conversion URLs, so I don't need the java.net.URLEncoder/URLDecoder classes.
[ May 03, 2007: Message edited by: Kevin Weisz ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

In JavaScript or on the server? I suspect the latter so I'm moving this to the Servlets forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kevin Weiss
Greenhorn

Joined: Mar 27, 2007
Posts: 7
It would be on the server. We cannot use Javascript in our case.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Originally posted by Kevin Weisz:
so I don't need the java.net.URLEncoder/URLDecoder classes.


Those wouldn't help you anyways; URL encoding and HTML Entity encoding are completely separate things.

There is no built-in encoder/decoder for HTML entitites. You'll either need to write your own or find a 3rd party implementation.

Depending upon how detailed you want to get (do you want to support all entitites, or just the critical ones like &, < and >) it can be as simple as a few calls to String.replaceAll().
[ May 03, 2007: Message edited by: Bear Bibeault ]
Kevin Weiss
Greenhorn

Joined: Mar 27, 2007
Posts: 7
At the moment, just the critical one like those you mentioned.
[ May 03, 2007: Message edited by: Kevin Weisz ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Then a simple class with methods that encode/decode through text substitution should be easy to whip up.

P.S. For emission onto a JSP page, <cut> will do this auto-matically.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Encoding and Decoding HTML characters
 
Similar Threads
html encode
Finding links in an HTML file
Return Two Rightmost Characters
Multi Line JLabel
Splitting the String to get all characters