org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x10)

开发XML-RPC 的时候出现了下面的错误:
org.apache.xmlrpc.XmlRpcException: Failed to parse XML-RPC request: An invalid XML character (Unicode: 0x10) was found in the element content of the document.

 Caused by: org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x10) was found in the element content of the document.
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xmlrpc.server.XmlRpcStreamServer.getRequest(XmlRpcStreamServer.java:65)

原因是xmlrpc客户端里将控制字符写进了xml,未进行处理,下面是将控制字符进行替换处理的代码:

  private static final Set hashSet;
 public static final char substitute = '\uFFFD';
    static {
        final String escapeString = "\u0000\u0001\u0002\u0003\u0004\u0005" +
            "\u0006\u0007\u0008\u000B\u000C\u000E\u000F\u0010\u0011\u0012" +
            "\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C" +
            "\u001D\u001E\u001F\uFFFE\uFFFF";

        hashSet = new HashSet();
        for (int i = 0; i < escapeString.length(); i++) {
         hashSet.add(escapeString.charAt(i));
        }
    }  
    private boolean isIllegal(char c) {
        return hashSet.contains(c);
    }

    /**
     * Substitutes all illegal characters in the given string by the value of
     * {@link EscapingXMLStreamWriter#substitute}. If no illegal characters
     * were found, no copy is made and the given string is returned.
     *
     * @param string
     * @return
     */
    private String escapeCharacters(String string) {

        char[] copy = null;
        boolean copied = false;
        for (int i = 0; i < string.length(); i++) {
            if (isIllegal(string.charAt(i))) {
                if (!copied) {
                    copy = string.toCharArray();
                    copied = true;
                }
                copy[i] = substitute;
            }
        }
        return copied ? new String(copy) : string;
    }


你可能感兴趣的:(java,xml,Google,other)