【问题标题】:Nokogiri XML builder with cyrillic text (windows 1251)带有西里尔文字的 Nokogiri XML 构建器(Windows 1251)
【发布时间】:2015-12-16 16:22:00
【问题描述】:

我正在尝试从数组创建 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

我的输入看起来像这样(它总是在第一个索引上包含一个文章编号,然后从第 2 个索引到 N-3 个索引进行描述,N-2 到 N-1 是数量,第 N 个索引包含条形码):

["047609", "СОК", "СВЕЖЕВЫЖАТЫЙ", "ТОМАТ", "200", "МЛ", "(фреш", "дня)", "1", "шт", "2400000032731"]["048504", "ВОДА", "ГАЗИРОВАННАЯ", "С", "НАТУРАЛЬНЫМ", "СИРОПОМ", "(200МЛ)", "1", "шт", "2400000032953"]["055794", "СОК", "СВЕЖЕВЫЖАТЫЙ", "В", "АССОРТИМЕНТЕ", "(200МЛ)", "1", "шт", "2400000036425"]["058270", "СОК", "СВЕЖЕВЫЖАТЫЙ", "КЛУБНИКА", "+ЯБЛОКО", "200", "МЛ", "(фреш", "дня)", "1", "шт", "2400000037149"]

这会导致这样的事情:

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

基本上,我希望 XML 中的描述显示正确的西里尔字母。

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

令人惊讶的是,如果我在我的文本中省略代码块,那么我的文本会很好地显示。

【问题讨论】:

  • 欢迎来到 Stack Overflow。请提供最少的输入数据和预期的输出。这有助于我们为您提供帮助,并让正在寻找类似答案的其他人了解您的代码如何适合他们的用例。
  • 另外,在 Ruby 中,方法和变量是用 snake_case 而不是 camelCase 编写的。 It's_a_matter_of_readability 和 CodeStyleInTheCommunity。
  • 你真的想要 force_encoding 而不是 encode 吗?
  • @FrederickCheung 我都试过了,但都没用。
  • @theTinMan 感谢您的反馈,更新了我的问题。

标签: ruby nokogiri cyrillic windows-1251


【解决方案1】:

经过数小时的尝试找到了这篇文章 - How do I encode/decode HTML entities in Ruby?

您需要根据下表解码&amp;#x421; 等值: http://webdesign.about.com/od/localization/l/blhtmlcodes-ru.htm

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 2022-12-03
    • 1970-01-01
    • 2012-11-02
    • 2021-06-09
    • 2013-06-02
    相关资源
    最近更新 更多