2012-03-07 57 views
2

實際上,我遇到了使用Rails 3.2.1在jruby-1.6.7中解析帶有REXML :: Document的XML字符串的問題。REXML :: Document.new無法使用US-ASCII編碼的XML解析

我有一個像

<?xml version="1.0" encoding="US-ASCII"?><a><b>Something</b></a> 

字符串,做一個

doc = REXML::Document.new(xml_string) 

截至目前,當使用JRuby的1.6.5.1工作,它失敗:

failed - can't convert NilClass into String 
backtrace of deliver! org/jruby/RubyIconv.java:207:in `initialize' 
org/jruby/RubyIconv.java:391:in `conv' 
/opt/jruby/lib/ruby/1.8/rexml/encodings/ICONV.rb:12:in `encode' 
/opt/jruby/lib/ruby/1.8/rexml/source.rb:55:in `encoding=' 
/opt/jruby/lib/ruby/1.8/rexml/source.rb:45:in `initialize' 
/opt/jruby/lib/ruby/1.8/rexml/source.rb:11:in `create_from' 
/opt/jruby/lib/ruby/1.8/rexml/parsers/baseparser.rb:121:in `stream=' 
/opt/jruby/lib/ruby/1.8/rexml/parsers/baseparser.rb:110:in `initialize' 
/opt/jruby/lib/ruby/1.8/rexml/parsers/treeparser.rb:9:in `initialize' 
/opt/jruby/lib/ruby/1.8/rexml/document.rb:227:in `build' 
/opt/jruby/lib/ruby/1.8/rexml/document.rb:43:in `initialize' 

將標題更改爲如下所示:

<?xml version="1.0" encoding="utf-8"?><a><b>Something</b></a> 

一切都很好。我錯過了我的系統上的一個lib,這是一個已知的問題嗎? US-ASCII無效?

回答

0

FWIW - 不是答案,但我也從JRuby 1.6.5/Rails 3.1.3到1.6.7/Rails 3.2.3,並且使用SimpleXML嘗試解析此xml時遇到了同樣的「新」錯誤:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt; 
&lt;simulacao&gt;&lt;ApoliceAuto Sistema=&quot;GISU&quot; Utilizador=&quot;USER&quot; 
ReferenciaExterna=&quot;16&quot; Ramo=&quot;90&quot; 
TipoSeguro=&quot;10&quot;&gt;&lt;Packs&gt;&lt;Pack 
codPack=&quot;1&quot;&gt;&lt;ValorAnualPack&gt;240.89&lt;/ValorAnualPack&gt;&lt; 
ValorSemestralPack&gt;124.39&lt;/ValorSemestralPack&gt;&lt; 
ValorContinuadoSemestralPack&gt; 
118.84&lt;/ValorContinuadoSemestralPack&gt;&lt;ValorTrimestralPack&gt;66.12&lt; 
/ValorTrimestralPack&gt;&lt;ValorContinuadoTrimestralPack&gt;60.57&lt; 
/ValorContinuadoTrimestralPack&gt;&lt;ValorMensalPack&gt;27.28&lt;/ValorMensalPack&gt;&lt; 
ValorContinuadoMensalPack&gt;21.73&lt;/ValorContinuadoMensalPack&gt;&lt;Mensagens/&gt;&lt; 
/Pack&gt;&lt;/Packs&gt;&lt;/ApoliceAuto&gt;&lt;/simulacao&gt; 

看起來像這樣(經由SO解析器:))

<?XML版本= 1.0 "編碼" = " ISO-8859-1 "? > <simulacao> < ApoliceAuto維拉克魯扎= " GISU " Utilizador = " USER " ReferenciaExterna = 拉莫= TipoSeguro = > <包> <包codPack = > <ValorAnualPack> 240.89 </ValorAnualPack > <ValorSemestralPack> 124.39 </Va lorSemestralPack > <ValorContinuadoSemestralPack> 118.84 </ValorContinuadoSemestralPack > <ValorTrimestralPack> 66.12 </ValorTrimestralPack > <ValorContinuadoTrimestralPack> 60.57 </ValorContinuadoTrimestralPack > <ValorMensalPack> 27.28 </ValorMensalPack > <ValorContinuadoMensalPack> 21.73 </ValorContinuadoMensalPack > < Mensagens/> < /包> < /包> </ApoliceAuto > </simulacao >