Ruby 带有西里尔文字的Nokogiri XML生成器(windows 1251)

Ruby 带有西里尔文字的Nokogiri XML生成器(windows 1251),ruby,nokogiri,cyrillic,windows-1251,Ruby,Nokogiri,Cyrillic,Windows 1251,我正在尝试从数组创建一个XML文件。这是我的生成器代码: def buildXML(formattedText) builder = Nokogiri::XML::Builder.new do |xml| xml.products { formattedText.each do |lineItem| xml.item { xml.articleNumber lineItem[0

我正在尝试从数组创建一个XML文件。这是我的生成器代码:

def buildXML(formattedText)
    builder = Nokogiri::XML::Builder.new do |xml|
        xml.products {
            formattedText.each do |lineItem|
                xml.item {
                    xml.articleNumber lineItem[0]
                    description = lineItem[1..(findIndexOnShtrih(lineItem)-1)].join(" ").force_encoding(Encoding::Windows_1251)
                    xml.description description
                    xml.shtrihCode lineItem.at(findIndexOnShtrih(lineItem))
                }
            end

        }
    end
end
我的输入是这样的(它总是在第一个索引上包含文章编号,然后从第二个索引到N-3个索引,N-2到N-1是数量,第N个索引包含条形码):

这导致了这样的情况:

    <articleNumber>055794</articleNumber>
    <description>&#x421;&#x41E;&#x41A; &#x421;&#x412;&#x415;&#x416;&#x415;&#x412;&#x42B;&#x416;&#x410;&#x422;&#x42B;&#x419; &#x412; &#x410;&#x421;&#x421;&#x41E;&#x420;&#x422;&#x418;&#x41C;&#x415;&#x41D;&#x422;&#x415; (200&#x41C;&#x41B;) 1 &#x448;&#x442;</description>
    <shtrihCode>2400000036425</shtrihCode>
  </item>
  <item>
    <articleNumber>058270</articleNumber>
    <description>&#x421;&#x41E;&#x41A; &#x421;&#x412;&#x415;&#x416;&#x415;&#x412;&#x42B;&#x416;&#x410;&#x422;&#x42B;&#x419; &#x41A;&#x41B;&#x423;&#x411;&#x41D;&#x418;&#x41A;&#x410; +&#x42F;&#x411;&#x41B;&#x41E;&#x41A;&#x41E; 200 &#x41C;&#x41B; (&#x444;&#x440;&#x435;&#x448; &#x434;&#x43D;&#x44F;) 1 &#x448;&#x442;</description>
    <shtrihCode>2400000037149</shtrihCode>
  </item>
</products>
055794
СОКСВЕЖЕВЫЖАТЫЙВАССОРТИМЕНТЕ;(200М;Л;)1шт;
2400000036425
058270
СОКСВЕЖЕВЫЖАТЫЙКЛУБНИКА+ЯБЛОКО;200МЛ;(ф;р;е;ш;д;н;я;)1шт;
2400000037149
基本上,我希望XML中的描述显示正确的西里尔字母

我可以强制构建器使用特定的编码吗?我找到了很多关于如何使用特定编码打开XML文件的资料,例如使用
Nokogiri::XML(a,nil,“UTF-8”)
,但没有找到关于如何构建有效XML的资料


令人惊讶的是,如果我省略了文本上的代码块,那么我的文本就可以正常显示。

经过几个小时的尝试,我发现了这篇文章-

您需要解码诸如
和#x421根据此表:

CGI没有帮我,但HTMLEntities帮了我

这是我现在的工作代码:

require 'htmlentities'
puts HTMLEntities.new.decode(buildXML(cleansedArray).to_xml)
最后是期望的输出:

  <item>
    <articleNumber>055794</articleNumber>
    <description>СОК СВЕЖЕВЫЖАТЫЙ В АССОРТИМЕНТЕ (200МЛ) 1 шт</description>
    <shtrihCode>2400000036425</shtrihCode>
  </item>
  <item>
    <articleNumber>058270</articleNumber>
    <description>СОК СВЕЖЕВЫЖАТЫЙ КЛУБНИКА +ЯБЛОКО 200 МЛ (фреш дня) 1 шт</description>
    <shtrihCode>2400000037149</shtrihCode>
  </item>
</products>

055794
СОК СВЕЖЕВЫЖАТЫЙ В АССОРТИМЕНТЕ (200МЛ) 1 шт
2400000036425
058270
СОК СВЕЖЕВЫЖАТЫЙ КЛУБНИКА +ЯБЛОКО 200 МЛ (фреш дня) 1 шт
2400000037149

欢迎来到堆栈溢出。请提供最小输入数据和预期输出。这有助于我们帮助您,并为正在搜索类似答案的其他人提供了您的代码如何适合他们的用例的想法。此外,在Ruby中,方法和变量是用snake_而不是camelCase编写的。这是社区中可读性和代码样式的问题。你真的想要强制编码而不是编码吗?@FrederickCheung我两种都试过了,但都没用。@theTinMan感谢你的反馈,更新了我的问题。
  <item>
    <articleNumber>055794</articleNumber>
    <description>СОК СВЕЖЕВЫЖАТЫЙ В АССОРТИМЕНТЕ (200МЛ) 1 шт</description>
    <shtrihCode>2400000036425</shtrihCode>
  </item>
  <item>
    <articleNumber>058270</articleNumber>
    <description>СОК СВЕЖЕВЫЖАТЫЙ КЛУБНИКА +ЯБЛОКО 200 МЛ (фреш дня) 1 шт</description>
    <shtrihCode>2400000037149</shtrihCode>
  </item>
</products>