saxparser 忽略字节顺序标记

发布于 2024-08-28 23:57:10 字数 87 浏览 4 评论 0 原文

我们的 saxparser 不会忽略出现在文件开头的字节顺序标记 

如何让我的 sax 解析器忽略字节顺序标记?

Our saxparser does not ignore the byte order mark which appears at the starting of the file.

How do I get my sax parser to ignore the byte order mark ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜深人未静 2024-09-04 23:57:10

在十六进制编辑器中检查该文件。

如果初始字节确实是 \xEF\xBB\xBF 后跟文档本身,那么它就是 UTF-8 人造 BOM。尽管 UTF-8 人造 BOM 是一种严重的错误,并且生成它们的工具需要使用特殊的编程酸来销毁,但 XML 规范确实要求解析器识别并忽略此字节序列,因此,如果您的 SAX 解析器不这样做,那么它就不兼容,需要一些改进。

如果为您提供的初始字节实际上类似于以下之一:

\xC3\xAF\xC2\xBB\xC2\xBF 
\xEF\xBB\xBF\xC3\xAF\xC2\xBB\xC2\xBF 
\xEF\x00\xBB\x00\xBF\x00
\xFF\xFE\xEF\x00\xBB\x00\xBF\x00

那么您所得到的是意外的双重编码。在这种情况下,您需要查看生成该文件的程序,因为它的格式不正确,SAX 解析器会正确地抱怨,并且文件中的其他 Unicode 字符也可能会混乱。可能它正在做一些愚蠢的事情,例如将文档序列化为字节字符串,然后通过虚假的解码/编码周期发送它。

无论哪种方式,如果您需要让解析器跳过麻烦的字节序列,则必须向其提供您手动破解以删除此前缀的版本。如果不知道您的 SAX 解析器是什么(甚至是什么语言),就很难说如何做到这一点。

也许您可以在将输入流传递给解析器之前查找输入流?也许您可以将文件读入字节字符串,然后将其(去掉初始字节)传递给解析器?如果您的解析器没有为您提供这些选项,您将必须以字节形式加载文件,剪辑开头并将其再次保存到新文件中。

Check the file in a hex editor.

If the initial bytes are indeed \xEF\xBB\xBF followed by the document itself, then it's a UTF-8 faux-BOM. Although UTF-8 faux-BOMs are an foul wrongness and tools that generate them need to be destroyed with special programming acid, the XML spec does require parsers to recognise and ignore this byte sequence, so if your SAX parser doesn't it's not compliant and needs some kicking.

If the initial bytes that are giving you  are actually something like one of:

\xC3\xAF\xC2\xBB\xC2\xBF 
\xEF\xBB\xBF\xC3\xAF\xC2\xBB\xC2\xBF 
\xEF\x00\xBB\x00\xBF\x00
\xFF\xFE\xEF\x00\xBB\x00\xBF\x00

then what you've got is an accidental double-encoding. In this case, you need to look at the program producing the file because it's not well-formed, a SAX parser would be correct to complain, and other Unicode characters in the file would probably be messed up too. Possibly it is doing something silly like serialising the document to a byte string then sending it through a bogus decode/encode cycle.

Either way, if you need to have the parser skip the troublesome byte sequence, you would have to feed it with a version you'd manually hacked to remove this prefix. Without knowing what you SAX parser is (or even what language) it's difficult to say how to do this.

Maybe you can seek the input stream before passing it to the parser? Maybe you can read the file into a byte string and pass that, shorn of the initial bytes, to the parser? If your parser doesn't give you those options you would have to load the file in as bytes, clip the beginning and save it out again to a new file.

北渚 2024-09-04 23:57:10

看起来您可能正在向 saxparser 提供 utf-16 输入,而该解析器不需要 utf-16。尝试将数据转换为utf-8,可能会有所帮助。

It looks like you may be giving utf-16 input to a saxparser which doesn't expect utf-16. Try to convert the data into utf-8, it might help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文