4

I'm building a web service.

Some one put illegal characters into our database.

Now when I try to retrieve those strings and send them via a webservice, the client chokes.

I get an error such as :

com.sun.xml.ws.encoding.soap.DeserializationException: Failed to read a response: javax.xml.bind.UnmarshalException
 - with linked exception:
[com.ctc.wstx.exc.WstxUnexpectedCharException: Illegal character ((CTRL-CHAR, code 18))

How can I remove this character in Java?

jeph perro
  • 6,242
  • 26
  • 90
  • 124
  • I'm looking for quick and dirty. Can I use something like this : stringName.replace('\u0022', ' ') – jeph perro Jan 20 '11 at 00:50
  • possible duplicate of [Best way to encode text data for XML in Java?](http://stackoverflow.com/questions/439298/best-way-to-encode-text-data-for-xml-in-java) – Taylor Leese Jan 20 '11 at 00:51
  • In the error message, does the `code 18` refer to the illegal character? `0x18 = 24 = control-char "CAN"` or `18 = 0x12 = control-char "DC2"` I've seen the "Illegal character" error before when people managed to get control characters into the database. – Stephen P Jan 20 '11 at 00:59
  • I have to assume it is DC2, although I'm not sure how that ever got into our database. – jeph perro Jan 20 '11 at 17:53
  • Neat solution with apache Xalan http://stackoverflow.com/a/9635310/489364 – kommradHomer May 20 '13 at 08:44

2 Answers2

3

Check this out:

stringName.replaceAll("[^\\p{Print}]", "");

Works like a charm.

jeph perro
  • 6,242
  • 26
  • 90
  • 124
3
/**
 * Function to strip control characters from a string.
 * Any character below a space will be stripped from the string.
 * @param iString the input string to be stripped.
 * @return a string containing the characters from iString minus any control characters.
 */
public String stripControlChars(String iString) {
    StringBuffer result = new StringBuffer(iString);
    int idx = result.length();
    while (idx-- > 0) {
        if (result.charAt(idx) < 0x20 && result.charAt(idx) != 0x9 && 
                result.charAt(idx) != 0xA && result.charAt(idx) != 0xD) {
            if (log.isDebugEnabled()) {
                log.debug("deleted character at: "+idx);
            }
            result.deleteCharAt(idx);
        }
    }
    return result.toString();
}
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147