2011-01-20 87 views
3

我正在构建一个Web服务。如何过滤出Java中的非法XML字符

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

现在,当我尝试检索这些字符串并通过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中这个角色?

+0

我正在寻找快速和肮脏。我可以使用像这样的东西:stringName.replace('\ u0022','') – 2011-01-20 00:50:13

回答

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(); 
} 
3

检查了这一点:

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

就像一个魅力。