replacing XML Special Characters

Posted on 2009-12-30
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
    LVL 5

    Accepted Solution

    String xmlValue="A&P"; // etc
    xmlValue=replaceAll("'","'").replaceAll("&","&"); // etc;
    LVL 8

    Expert Comment

    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:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Title # Comments Views Activity
    Core java. Please help me catch a bug. 5 49
    wordsWithoutList  challenge 24 60
    pairstar challenge 2 26
    computer science syllabus 3 37
    Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System ( introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
    I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now