java 替换 xml文件中无效的字符 标签: javaxml 2016-11-24 15:36 344人阅读 评论(0)

先上java替换xml无效字符的代码

public static String stripNonValidXMLCharacters(String in) {
        StringBuffer out = new StringBuffer(); // Used to hold the output.
        char current; // Used to reference the current character.

        if (in == null || ("".equals(in)))
            return ""; // vacancy test.
        for (int i = 0; i < in.length(); i++) {
            current = in.charAt(i); // NOTE: No IndexOutOfBoundsException caught
                                    // here; it should not happen.
            if ((current == 0x9) || (current == 0xA) || (current == 0xD)
                    || ((current >= 0x20) && (current <= 0xD7FF))
                    || ((current >= 0xE000) && (current <= 0xFFFD))
                    || ((current >= 0x10000) && (current <= 0x10FFFF)))
                out.append(current);
        }
        return out.toString();
    }

详情解释

XML文件中的合法Unicode字符范围(十六进制)

Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]

出现范围外的 字符 java解析的时候会报错 报 无效的字符 0x**
类似的错误。
先用上述方法过滤xml文件内容 吧无效的字符替换了 再去解析就正常了不会再报错了

posted @ 2016-11-24 15:36  xzcl  阅读(415)  评论(0编辑  收藏  举报