2

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

我有一个字符串

<?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>

一切安好。我是否错过了系统上的库,这是一个已知问题吗?US-ASCII 无效吗?

4

1 回答 1

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 version="1.0" encoding="ISO-8859-1"?> <simulacao><ApoliceAuto Sistema="GISU" Utilizador="USER" ReferenciaExterna="16" Ramo="90" TipoSeguro="10"> <Packs><Pack codPack="1"><ValorAnualPack>240.89</ValorAnualPack><ValorSemestralPack>124.39</ValorSemestralPack><ValorContinuadoSemestralPack>118.84</ValorContinuadoSemestralPack><ValorTrimestralPack>66.12</ValorTrimestralPack>Trimestral7</ValorTrimestralPack>Trimestral7</ValorTrimestralPack>60.5 ><ValorMensalPack>27.28</ValorMensalPack><ValorContinuadoMensalPack>21.73</ValorContinuadoMensalPack><Mensagens/></Pack></Packs></ApoliceAuto></simulacao>

于 2012-04-28T15:46:53.857 回答