REXML :: Document.new无法使用US-ASCII编码的XML进行解析

时间:2012-03-07 08:34:18

标签: ruby-on-rails ruby xml jruby rexml

实际上我遇到了使用Rails 3.2.1在jruby-1.6.7中使用REXML :: Document解析XML String的问题。

我有一个像

这样的字符串
<?xml version="1.0" encoding="US-ASCII"?><a><b>Something</b></a>

在执行

时曾经使用jruby-1.6.5.1
doc = REXML::Document.new(xml_string)

截至目前,它失败了:

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无效吗?

1 个答案:

答案 0 :(得分:0)

FWIW - 不是答案,但我也从JRuby 1.6.5 / Rails 3.1.3转到1.6.7 / Rails 3.2.3并且我使用SimpleXML尝试解析此xml时出现相同的“新”错误: / p>

&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解析器:))

&lt;?xml version =“1.0”encoding =“ISO-8859-1”?&gt; &lt; simulacao&gt;&lt; ApoliceAuto Sistema =“GISU”Utilizador =“USER”ReferenciaExterna =“16”Ramo =“90”TipoSeguro =“10”&gt;&lt; Packs&gt;&lt; Pack codPack =“1”&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; /包装&GT;&LT; /包&GT;&LT; / ApoliceAuto&GT;&LT; / simulacao&GT;