使用CDATA存储原始二进制流?
我想知道您是否可以直接存储,而不是 将二进制文件保存为 Base64 的开销使用 CDATA 将双字节二进制流转换为 XML 文件,或者将其注释掉,或者其他什么?
Instead of the overhead with saving binary as Base64, I was wondering if you could directly store double-byte binary streams into XML files, using CDATA, or commenting it out, or something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,您不能单独使用 CDATA 在 XML 文件中注入二进制数据。
在 XML1.0 中(因为 XML 1.1 更宽松,但不涉及控制字符),以下限制适用于 CDATA 字符:
这意味着有几个字符非法,其中包括:
除此之外,在没有 CDATA 的标准实体内容中:
é
可以,&zajdalkdza;
则不然)所以 CDATA 只是允许“<”、“>”的一种方式 和“&”,通过限制“]]>” 反而。 它没有解决非法 XML、Unicode 和 UTF-8 字符问题,这是主要问题。
解决方案:
No you can't use CDATA alone to inject binary data in an XML file.
In XML1.0 (because XML 1.1 is more permissive, but not about control chars), the following restrictions apply to CDATA characters:
That means there are several characters illegal, among them are:
In addition to that, in a standard entity content without CDATA :
é
is OK,&zajdalkdza;
is not)So CDATA is just a way to allow "<", ">" and "&", by restricting "]]>" instead. It doesn't solve the illegal XML, Unicode and UTF-8 characters issue which is the main problem.
Solutions:
Nul 字符(C 中的 '\0' )在 XML 中的任何位置都无效,即使作为转义符 ( & #0; ) 也是如此。
The Nul character ( '\0' in C ) is not valid anywhere in XML, even as an escape ( & #0; ).
XML 是一种纯文本格式 - 不要用它来存储二进制数据。 将二进制 blob 放入单独的文件中,并向引用这些文件的 XML 添加一个元素。 如果您想将所有二进制 blob 存储在单个文件中,请添加偏移量属性或类似的属性...
XML is a plain-text format - don't use it to store binary data. Put the binary blobs in separate files and add an element to your XML which references these files. If you want to store all binary blobs in a single file, add an offset attribute or something like that...
您可以将其存储为 CDATA,但存在某些字节序列将评估为关闭 CDATA 部分的有效 XML 的风险。 快速浏览一下 http://www. w3.org/TR/2006/REC-xml-20060816/#sec-cdata-sect,似乎您可以拥有除“]]>”之外的任何字符序列。 查看什么是有效的 XML 字符 也是。
You can store it as CDATA, but there's the risk that some byte sequences will evaluate to valid XML that closes the CDATA section. After a quick look at http://www.w3.org/TR/2006/REC-xml-20060816/#sec-cdata-sect, it seems you can have any sequence of chars except "]]>". Have a look at what is a valid XML char too.