Link to home
Start Free TrialLog in
Avatar of traveh
traveh

asked on

How to ampersand-escape an XML string

Hi,

Is there a standard java API method for ampersand-escaping a string (for using with XML)?

I do not want to create an XML document, but just to escape a string (creating a document with a Writer and all that would be over-kill).

If not - can anyone point me to some freeware class library that does that?
SOLUTION
Avatar of Webstorm
Webstorm

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mick Barry
String encoded = URLEncoder.encode(s);
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Webstorm
Webstorm


URLEncoder.encode method is for percent-escape parameters in URL, not for ampersand-escape.

My function is more simple and quick than the one at http://www.rgagnon.com/javadetails/java-0306.html because it escape accentued characters as "&#" code ";".
The whole set is needed only for decoding.

>>because it escape accentued characters as "&#" code ";".

AFAIK they should be escaped
Avatar of traveh

ASKER

Thanks, guys, I'll have to split the points here, since Webstorm's solution is good (and the first), but CEHJ's solution is more complete - that is, it has the decoding algorithm there too (though I didn't specifically ask for it).
Thanks for the points, traveh :-)

8-)