尝试读取和序列化具有UTF-16编码和声明的XML文档会导致Nokogiri在某个点之后产生垃圾。
C:\>nokogiri -v
# Nokogiri (1.5.5)
---
warnings: []
nokogiri: 1.5.5
ruby:
version: 1.9.3
platform: i386-mingw32
description: ruby 1.9.3p194 (2012-04-20) [i386-mingw32]
engine: ruby
libxml:
binding: extension
compiled: 2.7.7
loaded: 2.7.7
我有一个用UTF-16(LE)编码的XML文件,它还在顶部包含一个PI XML声明,表明编码是UTF-16。总结一下,它看起来像这样:
<?xml version="1.0" encoding="UTF-16" ?>
<Foo>
<Bar><![CDATA[
Lorem ipsum dolor ...about 3900 more bytes of content here...
]]></Bar>
<Jim>Oh! Hello there.</Jim>
</Foo>
当我使用Nokogiri阅读本文时,一切似乎都很好:
xml = File.open('Simplified.xml','rb:utf-16le',&:read)
p xml.encoding # #<Encoding:UTF-16LE>
p xml.valid_encoding? # true
doc1 = Nokogiri.XML(xml,&:noblanks)
xml1 = doc1.to_xml.encode('utf-8')
p xml1.encoding # #<Encoding:UTF-8>
p xml1.valid_encoding? # true
然而,序列化文档的输出在某一点之后变得很明显:
p xml1 # Correct contents of CDATA removed from the following output
#=> "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<Foo>\n <Bar><![CDATA[\n...\n\t]]></Bar>\n <Jim>Oh! Hello there.\uFFFE\u3C00\u0000\u2F00\u0000\u4A00\u0000\u6900\u0000\u6D00\u0000\u3E00\u0000\u0A00\u0000\u3C00\u0000\u2F00\u0000\u4600\u0000\u6F00\u0000\u6F00\u0000\u3E00\u0000\u0A00\u0000"
(限制似乎与字符数有关。我可以添加和删除Lorem ipsum文本中的一些单词而不做任何更改,但删除某个点下方的文本会突然修复输出。)< /子>
然而,Nokogiri文件并没有被打破。我可以成功地独立序列化<Jim>
:
puts doc1.at('Jim').to_xml.encode('utf-8')
#=> <Jim>Oh! Hello there.</Jim>
我发现的唯一解决方法是在解析之前删除文档顶部的XML声明。有了这个,一切都按照需要运作:
decl = '<?xml version="1.0" encoding="UTF-16" ?>'.encode('UTF-16LE')
doc2 = Nokogiri.XML(xml.sub(decl,''),&:noblanks)
puts doc2.to_xml.encode('utf-8')
#=> <?xml version="1.0"?>
#=> <Foo>
#=> <Bar><![CDATA[
#=> Lorem ipsum dolor...and more...
#=> ]]></Bar>
#=> <Jim>Oh! Hello there.</Jim>
#=> </Foo>
以下是您自己测试的完整文件:
<?xml version="1.0" encoding="UTF-16" ?>
<Foo>
<Bar><![CDATA[
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ac augue arcu, eget laoreet lorem. Quisque ac augue velit. Integer consectetur suscipit vehicula. Etiam et convallis enim. Etiam varius massa sit amet lacus rhoncus varius in non ante. Sed dictum, metus eu bibendum ornare, ligula dui commodo urna, ut dignissim felis dolor eget nisl. Proin sit amet nisi nunc. Vestibulum a urna sed dui dignissim blandit nec vel enim. Vivamus tincidunt nulla id dui hendrerit hendrerit.
Aliquam neque orci, luctus sit amet fringilla eu, varius vitae diam. Suspendisse varius rutrum lorem eget malesuada. Sed dapibus dapibus nisl, in cursus ante lacinia non. Aenean id sagittis ipsum. Suspendisse elit nunc, porta sit amet blandit ut, laoreet sed est. Nunc eget sem vitae nisl elementum ullamcorper ut sit amet urna. Sed ligula quam, fringilla in facilisis tincidunt, vehicula in nisi. Maecenas a augue in augue semper scelerisque sit amet ut arcu.
Praesent hendrerit, enim in elementum ornare, lorem nisi euismod dolor, sit amet ornare mi sem sodales lacus. Fusce et tempor mauris. In non quam nisl, non consequat diam. Duis sit amet massa ultrices massa cursus iaculis. Nunc ullamcorper malesuada sem dignissim semper. Fusce aliquet lacus quis nisi tincidunt sodales. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque posuere commodo aliquet. Aliquam blandit vestibulum facilisis. Sed pellentesque viverra dignissim. Etiam est lacus, mollis eu pretium vitae, lacinia eleifend augue. Mauris vitae quam nisl. In venenatis nunc ac eros elementum cursus.
Sed a metus sit amet nunc euismod condimentum id non orci. Curabitur velit turpis, lacinia non eleifend sed, rhoncus id est. Fusce ut massa dolor, ut sodales odio. Donec aliquam convallis tellus, eu pharetra tortor iaculis non. Integer imperdiet feugiat ipsum a gravida. Mauris sapien ipsum, ultricies ac placerat ut, imperdiet eu justo. Quisque quis consectetur velit. Etiam facilisis sapien nec enim tincidunt pulvinar. Duis fermentum faucibus felis, sed consequat libero pretium at. Phasellus nibh purus, suscipit in vestibulum vel, blandit at leo. Suspendisse placerat elit sed enim bibendum vel hendrerit mauris pretium. Maecenas ut lacus eu nisi euismod pretium.
Aliquam feugiat felis id massa aliquam pharetra sed non eros. Morbi interdum molestie iaculis. Curabitur varius ante ac dui dapibus non laoreet risus blandit. Nunc sit amet magna lacus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Phasellus egestas nunc sed turpis imperdiet a rhoncus massa aliquam. Nulla facilisi. Phasellus sit amet neque felis, nec vestibulum massa. Donec luctus fringilla dolor et gravida. Phasellus euismod lectus eget elit hendrerit non vehicula tellus venenatis. Phasellus sit amet ligula et purus dignissim feugiat at vitae libero. Proin ut tortor eros, quis laoreet lectus. Quisque nec urna mattis ante gravida fermentum eu at nibh. Phasellus sapien elit, tincidunt quis laoreet id, lobortis sed magna. Aliquam pulvinar erat eu sapien pretium bibendum. Maecenas eleifend, leo quis sodales tincidunt, leo felis tristique dolor, vitae ultrices neque felis ut metus.
Etiam dignissim egestas ipsum, eget tempor ipsum rutrum eu. Donec vehicula eleifend ullamcorper. Mauris justo nulla, varius a mattis a, cursus sit amet risus. Phasellus rutrum interdum blandit. Donec ut justo eros, ut auctor dolor. Suspendisse potenti. Cras ultricies, dui eget mattis bibendum, leo dui luctus purus, sit amet rhoncus libero metus eget purus. Pellentesque scelerisque ornare sapien faucibus tempor.
Suspendisse potenti. Proin fermentum bibendum dapibus. Pellentesque facilisis aliquam. Nam egestas tellus non mauris scelerisque feugiat pellentesque lacus dignissim. Quisque id nulla felis. Mauris justo mauris, posuere sed facilisis in, venenatis nec risus. Mauris eu dui sed tellus laoreet tempor a in turpis volutpat.
]]></Bar>
<Jim>Oh! Hello there.</Jim>
</Foo>
答案 0 :(得分:3)
您可以指定要在options to to_xml
中使用的编码,而不是序列化xml然后在字符串上调用encode
。而不是
xml1 = doc1.to_xml.encode('utf-8')
使用:
xml1 = doc1.to_xml(:encoding => 'utf-8')
这似乎可以解决问题。
至于发生了什么,我只能提供一些观察。
首先,to_xml
生成的字符串编码没有指定编码是UTF-16
,在Ruby中是一个“虚拟编码”(无论这意味着什么):
xml1 = doc1.to_xml
p xml1.encoding
#=> #<Encoding:UTF-16 (dummy)>
文档说明dummy encodings:
虚拟编码是未正确实现字符处理的编码。它用于有状态编码。
我注意到的另一件事是输出的munged部分中的值实际上对应于应该出现的代码点。
Hello there.\uFFFE\u3C00\u0000\u2F00\u0000\u4A00\u0000\u6900...
3C
为<
,2F
为/
,4A
为J
,69
为i
等等,产生(如果忽略零和额外的BOM)
Hello there.</Ji...
如果你在编码为UTF-8之前写出Nokogiri生成的XML并指向它的十六进制编辑器,那么开头看起来像这样:
0000000 ff fe 3c 00 3f 00 78 00 6d 00 6c 00 20 00 76 00
它以FF FE
开头,即一个小端BOM。
在重新开始时,它看起来像这样:
0001f20 3c 00 4a 00 69 00 6d 00 3e 00 4f 00 68 00 21 00
0001f30 20 00 48 00 65 00 6c 00 6c 00 6f 00 20 00 74 00
0001f40 68 00 65 00 72 00 65 00 2e 00 fe ff 00 3c 00 00
0001f50 00 2f 00 00 00 4a 00 00 00 69 00 00 00 6d 00 00
0001f60 00 3e 00 00 00 0a 00 00 00 3c 00 00 00 2f 00 00
fe ff
是munged输出开始的地方(在中间一行)。 fe ff
也是 big endian BOM,其他字符似乎是BE(您可以看到fe ff
之前和之后的零列如何排列。但是在字符之间还有一对零字节。