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
& --> "" 
"  "  --> _
emadhuAsked:
Who is Participating?
 
TomLunCommented:
String xmlValue="A&P"; // etc
xmlValue=replaceAll("'","'").replaceAll("&","&"); // etc;
0
 
ozlevanonCommented:
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/
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.