Dom4j 读 xml 时,遇到 xml 无效字符,报错:An invalid XML character

来源:互联网 发布:初中英语课文朗读软件 编辑:程序博客网 时间:2024/06/05 10:09

它说xml有无效字符,然后找一下,xml 的无效字符有那些,它有三段,官方定义的无效字符为:

  1. 0x00 - 0x08
  2. 0x0b - 0x0c
  3. 0x0e - 0x1f

 

 

 

1:

str.replaceAll("[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]""");

 

2:

public static String filter(String xmlStr) {  StringBuilder sb = new StringBuilder();  char[] chs = xmlStr.toCharArray();  // System.out.println("filter before=" +chs.length);  for (char ch : chs) {   if ((ch >= 0x00 && ch <= 0x08) || (ch >= 0x0b && ch <= 0x0c)     || (ch >= 0x0e && ch <= 0x1f)) {   } else {    sb.append(ch);   }  }  // System.out.println("filter after=" +sb.length());  return sb.toString(); }


 

原创粉丝点击