Java 怎么用空格替换给定数组中的错误XML字符

2019年8月3日23:22:38Java 怎么用空格替换给定数组中的错误XML字符已关闭评论 417 642字阅读2分8秒

1.简介

Java 怎么用空格替换给定数组中的错误XML字符?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */



public class Main {
    public static void main(String[] argv) throws Exception {
        char[] cbuf = new char[] { 'b', 'o', 'o', 'k', '2', 's', '.', 'c',
                'o', 'm', 'a', '1', };
        int off = 2;
        int len = 2;
        replaceBadXmlCharactersBySpace(cbuf, off, len);
    }

    
    public static void replaceBadXmlCharactersBySpace(char[] cbuf, int off,
            int len) {
        for (int i = off; i < off + len; i++) {
            if (isBadXmlCharacter(cbuf[i])) {
                cbuf[i] = '\u0020';
            }
        }
    }

    
    public static boolean isBadXmlCharacter(char c) {
        boolean cDataCharacter = c < '\u0020' && c != '\t' && c != '\r'
                && c != '\n';
        cDataCharacter |= (c >= '\uD800' && c < '\uE000');
        cDataCharacter |= (c == '\uFFFE' || c == '\uFFFF');
        return cDataCharacter;
    }
}

 
编程十万个怎么办