Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Html encoding  RSS feed

 
Stee Renwick
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Quick question about encoding. Does anyone know of a standard API (something in Java/xalan/xerces) that I could use to encode (format) as string for HTML. In other words something that would convert reserved characters like "<" or ">" or " " to "<", ">" and so on. I've had a look at the API's but nothing seems to jump out at me - that said, I would have thought it was a common thing programers in this area would need.

Thanks

p.s if this isn't the right forum to stick this in - sorry and which one should I use.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"<" or ">" or " " to "<", ">"

Hmm. Hard to follow, but I think I know what you need: there's nothing in the standard API's to do this though. You might like to take a look at the code for Mozilla. It must be doing this somewhere, might give you a few hints.
 
Stee Renwick
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry,
Just looking back on my post, and your right, its not very clear. What I'm looking for is an API that I can pass a string into. it will then check that string for anything like '<' or '>' and so on, converting any it finds into html friendly escape characters (I think thats what there called) like '<' for '<' and similarly replacing '>' with '>'. I think there are a load of characters that could potentially cause problems (as the browser may mis-interpret them or ignore them. So by converting them they will apear as normal. Kind of a bit like putting everything in a <pre> tag, where the brower will ignore the contents. (its just that I can't do that for everything). I reckon there must be an API out there that will do this, as I presume its a common enough problem, when dynamically generating html/xhtml content.

Thanks
Stee
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are only a handful of such characters that should be changed to their HTML Entity equivalents. Off the top of my head I'd recommend converting the following set: < > & and perhaps ".

There is no standard 'HTML encoder' API, but it's easy enough to build one using the methods on the String object.

Change:
< to &lt;
> to &gt;
& to &amp;
[ November 10, 2004: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Btw, on a JSP page, use of the JSTL <cut> action automatically performs this encoding for you.
 
Stee Renwick
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,
I was thinking of just throwing something together, but out of fear of forgetting some of them, and also that whole globalisation thing, I reckoned it would be safer to just reused one written by someone more knowledgable in these areas :-)
But as you said, it only seems to be a few characters, sure what could go wrong...

Thanks a mil,

p.s. I'm not actually using JSP, I generating the code myself - I know, don't ask - oh the fun I'm having :-(
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use this:


${ fn:escapeXml( sHtmlHostile ) }
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!