以 word 做模板时,文档中的字符串在 xml 中可能被打散到多个节点。
给出上下界字符(比如 {} ),去除中间无用的 dom 节点,使其之间的内容在 xml 中是连续完整的字符串。
用双指针标出上下界后对其中的 dom 节点进行替换。
public static void format(String inPath, String outPath, char beginFlag, char endFlag) throws Exception { InputStream in = new FileInputStream(inPath); OutputStream outputStream = new FileOutputStream(outPath); StringBuilder stringBuilder = new StringBuilder(); byte[] bytes = new byte[1024]; String pre = null; int len = -1; while ((len = in.read(bytes)) != -1) { byte[] bytes2 = new byte[len]; for (int k = 0; k < len; k++) bytes2[k] = bytes[k]; String content = new String(bytes2, "UTF-8"); stringBuilder.append(content); } in.close(); int left = 0, right = 0; while (left < stringBuilder.length()) { if (right > left) { if (stringBuilder.charAt(right) == endFlag) { formatString(stringBuilder, left, right); right++; left = right; } else right++; } else { if (stringBuilder.charAt(left) == beginFlag) right++; else { left++; right++; } } } String s = stringBuilder.toString(); if (s.contains("$")) s = s.replaceAll("\\$", ""); byte[] bytes1 = s.getBytes("UTF-8"); outputStream.write(bytes1, 0, bytes1.length); outputStream.flush(); outputStream.close(); } private static void formatString(StringBuilder stringBuilder, int left, int right) { boolean isDel = false; for (int i = left; i < right; i++) { if (isDel) { if (stringBuilder.charAt(i) == '>') isDel = false; stringBuilder.setCharAt(i, '$'); } else if (stringBuilder.charAt(i) == '<') { stringBuilder.setCharAt(i, '$'); isDel = true; } } }