blog迁移至:
http://www.micmiu.com
我们在解析XML文件时,会碰到程序发生以下一些异常信息:
引用
org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x{2}) was found in the value of attribute "{1}" and element is "1f".
引用
An invalid XML character (Unicode: 0x1d) was found in the CDATA section.
这些错误的发生是由于一些不可见的特殊字符的存在,而这些字符对于XMl文件来说又是非法的,所以XML解析器在解析时会发生异常,官方定义了XML的无效字符分为三段:
- 0x00 - 0x08
- 0x0b - 0x0c
- 0x0e - 0x1f
解决方法是:在解析之前先把字符串中的这些非法字符过滤掉:
string.replaceAll("[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]", "")
测试代码:TestXmlInvalidChar.java
package michael.xml;
import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* @author michael
*
*/
public class TestXmlInvalidChar {
/**
* @param args
*/
public static void main(String[] args) {
// 测试的字符串应该为:<r><c d="s" n="j"></c></r>
// 正常的对应的byte数组为
byte[] ba1 = new byte[] { 60, 114, 62, 60, 99, 32, 100, 61, 34, 115,
34, 32, 110, 61, 34, 106, 34, 62, 60, 47, 99, 62, 60, 47, 114,
62 };
System.out.println("ba1 length=" + ba1.length);
String ba1str = new String(ba1);
System.out.println(ba1str);
System.out.println("ba1str length=" + ba1str.length());
System.out.println("-----------------------------------------");
// 和正常的byte 数组相比 多了一个不可见的 31
byte[] ba2 = new byte[] { 60, 114, 62, 60, 99, 32, 100, 61, 34, 115,
34, 32, 110, 61, 34, 106, 31, 34, 62, 60, 47, 99, 62, 60, 47,
114, 62 };
System.out.println("ba2 length=" + ba2.length);
String ba2str = new String(ba2);
System.out.println(ba2str);
System.out.println("ba2str length=" + ba2str.length());
System.out.println("-----------------------------------------");
try {
DocumentBuilderFactory dbfactory = DocumentBuilderFactory
.newInstance();
dbfactory.setIgnoringComments(true);
DocumentBuilder docBuilder = dbfactory.newDocumentBuilder();
// 过滤掉非法不可见字符 如果不过滤 XML解析就报异常
String filter = ba2str.replaceAll(
"[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]", "");
System.out.println("过滤后的length=" + filter.length());
ByteArrayInputStream bais = new ByteArrayInputStream(filter
.getBytes());
Document doc = docBuilder.parse(bais);
Element rootEl = doc.getDocumentElement();
System.out.println("过滤后解析正常 root child length="
+ rootEl.getChildNodes().getLength());
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试代码运行结果如下:
引用
ba1 length=26
<r><c d="s" n="j"></c></r>
ba1str length=26
-----------------------------------------
ba2 length=27
<r><c d="s" n="j"></c></r>
ba2str length=27
-----------------------------------------
过滤后的length=26
过滤后解析正常 root child length=1
对比可见,byte数组及字符串的长度前后是不一样的,但打印到控制台显示的结果却是一样的。同样过滤之后的字符串长度是有变化的。
-----------------------------------分 ------------------------------------隔 ------------------------------------线 --------------------------------------
分享到:
相关推荐
好多版本是会报错的,[Fatal Error] :24:28: An invalid XML character (Unicode: 0xd863) was found in the element content of the document. org.xml.sax.SAXParseException: An invalid XML character (Unicode:...
invalid multibyte character sequence 870告警1
好多版本是会报错的,[Fatal Error] :24:28: An invalid XML character (Unicode: 0xd863) was found in the element content of the document. org.xml.sax.SAXParseException: An invalid XML character (Unicode:...
NULL 博文链接:https://fish-bone.iteye.com/blog/1732229
今天在项目中,使用Mybatis对oracle数据库进行操作的时候,报出ORA-00911: invalid character的错误,检查了一下SQL,发现都书写正确啊,复制到plsql上执行也都没问题,这什么原因呢? 注意:这里说的是用navicat...
1、ValueError: Invalid control character at: line 1 column 8363 (char 8362) 使用json.loads(json_data)时,出现: ValueError: Invalid control character at: line 1 column 8363 (char 8362) 出现错误的...
java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986 at org.apache.coyote....
TypeError: ‘required’ is an invalid argument for positionals 的解决方法 当我在使用argparse模块时,遇到了如下错误: import argparse parser = argparse.ArgumentParser(description = 'debug_example') ...
NULL 博文链接:https://wilian.iteye.com/blog/1992365
sarscape软件教程,卫星的相关参数和卫星的影像特点和参数
新增了一个glibc-2.12-1.149.el6_6.5.i686.rpm包,其他的和之前的更新包相同,部分系统没有安装过这个包,直接批量更新可能回报错,说少个包。特此补上
This is not an invalid description.
Document.createElementNS with an empty qualified name should cause an INVALID_CHARACTER_ERR.
jquery.bgiframe.js在IE9下的错误 代码如下: SCRIPT5022: DOM Exception: INVALID_CHARACTER_ERR (5) jquery.bgiframe.js, 行8 字符976 错误代码 代码如下: 1 {if(!$(‘iframe.bgiframe’,this)[0])this....
求问各位大神,在仿真是出现以下问题:Evaluation of expression resulted in an invalid output. Only finite double vector or matrix outputs are supported。估计是.m文件调用的问题,求解释,拜谢拜谢。
PHP中GBK页面调用WebService的编码问题:XML error parsing SOAP payload on line 1
S32K144_ Keil MDK开发 Freertos例程
java.lang.IllegalStateException: Invalid name=“com.alibaba.dubbo.config.ProtocolConfig#0” contains illegal character, only digit, letter, ‘-’, ‘_’ or ‘.’ is legal 原因: 如果没有指定id属性,...