关于XML字符串中出现非法字符导致解析出错的问题

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_15784927/article/details/78972176

最近调用接口报大量错误org.apache.cxf.interceptor.Fault: Marshalling Error: Invalid white space character (0x2) in text to output (in xml 1.1, could output as a character entity),根据提示可知出现了非法字符导致解析出错。查看日志果真如此,由于使用的是XML1.0,所以需要对报文字符串进行过滤,根据xml1.0规范https://www.w3.org/TR/REC-xml/可知xml1.0字符集范围如下Char       ::=       #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]。于是进行字符串过滤

public static String xmlFilter(String str){
		StringBuffer out = new StringBuffer();
	    char current;
	    for (int i = 0; i < str.length(); i++) {
	        current = str.charAt(i);
	        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();
	}


展开阅读全文

没有更多推荐了,返回首页