Link to home
Start Free TrialLog in
Avatar of emadhu
emadhu

asked on

replacing XML Special Characters

We have a program which reads the data from database and generates XML messages

Some data items have special characters like "A&P" , "G  E". Since these characters are not allowed as XML element names. CAn you some point me to a code snippet or library which
takes a String and replaces all occurrences of "&", " ",  "  " with corresponding characters.

Example: The one on the left should be replaced with the one on the right
& --> "" 
"  "  --> _
ASKER CERTIFIED SOLUTION
Avatar of TomLun
TomLun
Flag of Sweden image

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 ozlevanon
ozlevanon

You can try and use Apache's StringEscapeUtils, which has methods for these kind of things (specifically you need escapeXml method): http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/StringEscapeUtils.html
The class is part of Apache's commons lang project, you can download it here:http://commons.apache.org/lang/