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
& --> "" 
"  "  --> _
Who is Participating?
String xmlValue="A&P"; // etc
xmlValue=replaceAll("'","'").replaceAll("&","&"); // etc;
You can try and use Apache's StringEscapeUtils, which has methods for these kind of things (specifically you need escapeXml method):
The class is part of Apache's commons lang project, you can download it here:
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.