replacing XML Special Characters

Posted on 2009-12-30
Medium Priority
Last Modified: 2013-11-23
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
& --> "" 
"  "  --> _
Question by:emadhu

Accepted Solution

TomLun earned 2000 total points
ID: 26146496
String xmlValue="A&P"; // etc
xmlValue=replaceAll("'","'").replaceAll("&","&"); // etc;

Expert Comment

ID: 26146876
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/

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month16 days, 3 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question