如何在Java中过滤掉非法的XML字符

时间:2011-01-20 00:45:44

标签: java xml web-services string soap

我正在构建一个Web服务。

有人将非法字符放入我们的数据库。

现在,当我尝试检索这些字符串并通过Web服务发送它们时,客户端会窒息。

我收到如下错误:

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))

如何在Java中删除此字符?

2 个答案:

答案 0 :(得分:3)

检查出来:

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

像魅力一样。

答案 1 :(得分: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();
}