• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Encoding and Decoding HTML characters

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In JavaScript or on the server? I suspect the latter so I'm moving this to the Servlets forum.
 
Kevin Weiss
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would be on the server. We cannot use Javascript in our case.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the moment, just the critical one like those you mentioned.
[ May 03, 2007: Message edited by: Kevin Weisz ]
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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, <c:out> will do this auto-matically.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic