发现无效的 XML 字符(Unicode:0xc)
使用 Java DOM 解析器解析 XML 文件会产生:
[Fatal Error] os__flag_8c.xml:103:135: An invalid XML character (Unicode: 0xc) was found in the element content of the document.
org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0xc) was found in the element content of the document.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
Parsing an XML file using the Java DOM parser results in:
[Fatal Error] os__flag_8c.xml:103:135: An invalid XML character (Unicode: 0xc) was found in the element content of the document.
org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0xc) was found in the element content of the document.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
即使您将数据封装在 CDATA 块中,也有一些字符在 XML 文档中是不允许的。
如果您生成了文档,则需要对其进行
实体编码或删除。如果您有错误的文档,您应该在尝试解析它之前删除这些字符。请参阅 dolmens 在此线程中的回答:XML 中的无效字符
他链接到本文的位置:http://www.w3.org/TR/xml/#charsets
基本上,所有字符以下不允许使用 0x20,但 0x9 (TAB)、0xA (CR?)、0xD (LF?) 除外
There are a few characters that are dissallowed in XML documents, even when you encapsulate data in CDATA-blocks.
If you generated the document you will need to
entity encode it orstrip it out. If you have an errorneous document, you should strip away these characters before trying to parse it.See dolmens answer in this thread: Invalid Characters in XML
Where he links to this article: http://www.w3.org/TR/xml/#charsets
Basically, all characters below 0x20 is disallowed, except 0x9 (TAB), 0xA (CR?), 0xD (LF?)
每当无效的 xml 字符出现在 xml 中时,就会出现这样的错误。当你在记事本++中打开它时,它看起来像 VT、SOH、FF,就像这些是无效的 xml 字符。我正在使用 xml 版本 1.0,并且在按模式将文本数据输入数据库之前验证文本数据
它将确保不会在 xml 中输入无效的特殊字符
Whenever invalid xml character comes xml, it gives such error. When u open it in notepad++ it look like VT, SOH,FF like these are invalid xml chars. I m using xml version 1.0 and i validate text data before entering it in database by pattern
It will ensure that no invalid special char will enter in xml
字符 0x0C 在 XML 1.0 中无效,但在 XML 1.1 中是有效字符。因此,除非 xml 文件在序言中将版本指定为 1.1,否则它就是无效的,您应该向该文件的生产者投诉。
The character 0x0C is be invalid in XML 1.0 but would be a valid character in XML 1.1. So unless the xml file specifies the version as 1.1 in the prolog it is simply invalid and you should complain to the producer of this file.
您可以使用自定义 FilterReader 类过滤所有“无效”字符:
并像这样运行它:
You can filter all 'invalid' chars with a custom FilterReader class:
And run it like this:
在 UTF-8 上,不允许使用这些范围内的所有代码,对于 XML 1.0:
可以删除 then 的正则表达式为:
注意:如果如果您正在使用 XML 1.1,您还需要删除这些间隔:
参考:
On UTF-8, all the codes on these ranges are not allowed, for XML 1.0:
A regex that can remove then is:
Note: if you are working with XML 1.1, you also need to remove these intervals:
Refs:
我刚刚使用了这个项目,发现它非常方便: https://github.com/rwitzel/streamflyer
如文档所述,使用 InvalidXmlCharacterModifier。
就像这个例子:
I just used this project, and found it very handy: https://github.com/rwitzel/streamflyer
Using the InvalidXmlCharacterModifier, as the documentation says.
Like this example:
我遇到了类似的问题,XML 包含控制字符。查看代码后,我发现使用了一个已弃用的类 StringBufferInputStream 来读取字符串内容。
http://docs.oracle.com/javase/7 /docs/api/java/io/StringBufferInputStream.html
我将其更改为 ByteArrayInputStream 并且工作正常。
I faced a similar issue where XML was containing control characters. After looking into the code, I found that a deprecated class,StringBufferInputStream, was used for reading string content.
http://docs.oracle.com/javase/7/docs/api/java/io/StringBufferInputStream.html
I changed it to ByteArrayInputStream and it worked fine.
对于将字节数组读入 String 并尝试使用 JAXB 转换为对象的人,您可以通过从字节数组创建 String 来添加“iso-8859-1”编码,如下所示:
String JAXBallowedString= new String(byte[] input, " ISO-8859-1”);
这会将冲突的字节替换为 JAXB 可以处理的单字节编码。显然这个解决方案只是解析xml。
For people who are reading byte array into String and trying to convert to object with JAXB, you can add "iso-8859-1" encoding by creating String from byte array like this:
String JAXBallowedString= new String(byte[] input, "iso-8859-1");
This would replace the conflicting byte to single-byte encoding which JAXB can handle. Obviously this solution is only to parse the xml.
所有这些答案似乎都假设用户正在生成错误的 XML,而不是从 gSOAP 接收它,gSOAP 应该更清楚!
All of these answers seem to assume that the user is generating the bad XML, rather than receiving it from gSOAP, which should know better!
今天,我遇到了类似的错误:
Servlet.service() for servlet [remoting] in context with path [/***] throwed exception [请求处理失败;嵌套异常是 java.lang.RuntimeException: buildDocument failed.] 其根本原因
org.xml.sax.SAXParseException;行号:19;列数:91;在属性“text”的值中发现无效的 XML 字符(Unicode:0xc),元素为“label”。
在我第一次遇到错误后,我手动重新输入了整行,所以特殊字符无法潜入,并且 Notepad++ 没有显示任何不可打印的字符(白底黑字),尽管如此,我一遍又一遍地遇到相同的错误。
当我查找我所做的与前任不同的事情时,结果发现这是在结束之前多了一个空格 /> (据我所知,推荐用于较旧的解析器,但根据 XML 标准,无论如何它都不会有任何区别):
当我删除空格时:
一切正常。
所以这绝对是一个误导性的错误消息。
Today, I've got a similar error:
Servlet.service() for servlet [remoting] in context with path [/***] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: buildDocument failed.] with root cause
org.xml.sax.SAXParseException; lineNumber: 19; columnNumber: 91; An invalid XML character (Unicode: 0xc) was found in the value of attribute "text" and element is "label".
After my first encouter with the error, I had re-typed the entire line by hand, so that there was no way for a special character to creep in, and Notepad++ didn't show any non-printable characters (black on white), nevertheless I got the same error over and over.
When I looked up what I've done different than my predecessors, it turned out it was one additional space just before the closing /> (as I've heard was recommended for older parsers, but it shouldn't make any difference anyway, by the XML standards):
<label text="this label's text" layout="cell 0 0, align left" />
When I removed the space:
<label text="this label's text" layout="cell 0 0, align left"/>
everything worked just fine.
So it's definitely a misleading error message.
org.xml.sax.SAXParseException
包含现有的无效字符行号和列号。要捕获并记录此详细信息:
org.xml.sax.SAXParseException
contains existing invalid character line number and column number.For catching and logging this details: