2016-11-30 16 views
0

データベースオブジェクトの配列@configsがXML形式に変換されていますが、出力が期待通りではありません。すべてのエントリは、<entry>タグの代わりに<map>タグで囲まれています。私は<tag>をXMLルートにしたかっただけです。 <tag>ルートを使用してXMLを構築し、すべてのエントリを<entry>タグに入れるにはどうすればよいですか? ご協力いただきありがとうございます!ここで ハッシュの配列をRailsでXMLに変換するには?

は私のコードです:

entries = Array.new 
    entry = Hash.new 
    conf = Hash.new 

    @configs.each do |config| 

     entry.store('string', config.key) 

     conf.store('value', config.value) 
     conf.store('comment', config.comment) 

     entry.store('com.mirth.connect.util.ConfigurationProperty', conf) 

     entries << entry  

    end 

    pp entries.to_xml(:root => 'map', :indent => 0, :skip_types => true) 

、結果は次のとおりです。

<?xml version=\"1.0\" encoding=\"UTF-8\"?> 
<map> 
    <map> 
     <string>PNB_ALERTLOG_RECEIVER_CHANNEL</string> 
     <com.mirth.connect.util.ConfigurationProperty> 
      <value>PNB_ALERTLOG_RECEIVER</value> 
      <comment>Canal que irá receber tudo o que for logged com Warning e Error</comment> 
     </com.mirth.connect.util.ConfigurationProperty> 
    </map> 
    <map> 
     <string>PNB_CFG_FILE_ACCESS_CONTROL</string> 
     <com.mirth.connect.util.ConfigurationProperty> 
      <value>resources/configPnbDev/pnbAccessControl.json</value> 
      <comment>Este ficheiro permite configurar Autenticação e Controlo de Acessos.</comment> 
     </com.mirth.connect.util.ConfigurationProperty> 
    </map> 
    <map> 
     <string>PNB_CFG_FILE_CONNECTION_POOLS</string> 
     <com.mirth.connect.util.ConfigurationProperty> 
      <value>resources/configPnbDev/pnbConnectionPools.json</value> 
      <comment>Configuração de Oracle Universal Connection Pools usadas pelo PNB (PEM, RCU2)</comment> 
     </com.mirth.connect.util.ConfigurationProperty> 
    </map> 
    <map> 
     <string>PNB_CFG_FILE_CSP_MC_EXCLUSIONS</string> 
     <com.mirth.connect.util.ConfigurationProperty> 
      <value>resources/configPnbDev/medCronExclusions/mcExclCurrentRevision.json</value> 
      <comment>N/A</comment> 
     </com.mirth.connect.util.ConfigurationProperty> 
    </map> 
    <map> 
     <string>PNB_CFG_FILE_FACILITIES_ALIAS</string> 
     <com.mirth.connect.util.ConfigurationProperty> 
      <value>resources/configPnbDev/snsFacilitiesAlias.json</value> 
      <comment>Mapa de alias do codigo das instituicoes do SNS.</comment> 
     </com.mirth.connect.util.ConfigurationProperty> 
    </map> 
</map> 

私が何を望むか:

<?xml version=\"1.0\" encoding=\"UTF-8\"?> 
<map> 
    <entry> 
     <string>PNB_ALERTLOG_RECEIVER_CHANNEL</string> 
     <com.mirth.connect.util.ConfigurationProperty> 
      <value>PNB_ALERTLOG_RECEIVER</value> 
      <comment>Canal que irá receber tudo o que for logged com Warning e Error</comment> 
     </com.mirth.connect.util.ConfigurationProperty> 
    </entry> 
    <entry> 
     <string>PNB_CFG_FILE_ACCESS_CONTROL</string> 
     <com.mirth.connect.util.ConfigurationProperty> 
      <value>resources/configPnbDev/pnbAccessControl.json</value> 
      <comment>Este ficheiro permite configurar Autenticação e Controlo de Acessos.</comment> 
     </com.mirth.connect.util.ConfigurationProperty> 
    </entry> 
    <entry> 
     <string>PNB_CFG_FILE_CONNECTION_POOLS</string> 
     <com.mirth.connect.util.ConfigurationProperty> 
      <value>resources/configPnbDev/pnbConnectionPools.json</value> 
      <comment>Configuração de Oracle Universal Connection Pools usadas pelo PNB (PEM, RCU2)</comment> 
     </com.mirth.connect.util.ConfigurationProperty> 
    </entry> 
    <entry> 
     <string>PNB_CFG_FILE_CSP_MC_EXCLUSIONS</string> 
     <com.mirth.connect.util.ConfigurationProperty> 
      <value>resources/configPnbDev/medCronExclusions/mcExclCurrentRevision.json</value> 
      <comment>N/A</comment> 
     </com.mirth.connect.util.ConfigurationProperty> 
    </entry> 
    <entry> 
     <string>PNB_CFG_FILE_FACILITIES_ALIAS</string> 
     <com.mirth.connect.util.ConfigurationProperty> 
      <value>resources/configPnbDev/snsFacilitiesAlias.json</value> 
      <comment>entrya de alias do codigo das instituicoes do SNS.</comment> 
     </com.mirth.connect.util.ConfigurationProperty> 
    </entry> 
</map> 

答えて

2

はこれを試してみてください。

pp entries.to_xml(:root => 'map', :children => 'entry', :indent => 0, :skip_types => true) 

ソース:あなたが書いた場合

entry = { 
    a: “hello”, 
    b: “goodbye”, 
} 

http://apidock.com/rails/Array/to_xml

+0

ありがとうございます@mr_sudaca、それは働いた! :) – Ayanami

1

と仮定エントリは以下のハッシュある

entries = [] 
entries << entry 
p entries 

、出力は次のとおりです。

[{:a => “hello”, {:b => “goodbye”}] 

もしそうなら、あなたはその後、書く:

p entries.to_xml 

「エントリ」という単語が出力に表示されるとはどういうことでしょうか?これは、次のような出力を期待するようなものです。

x = 10 
y = 20 
puts x+y 

「x」と「y」をどこかに含めるようなものです。

アレイ用to_xml()のドキュメントによると:

各要素に対してto_xml呼び出すことで...文字列を返します。
オプションのハッシュが下に渡されます。
http://apidock.com/rails/Array/to_xml

オプションのハッシュを下向きに渡されるということは、あなたがアレイ上to_xml()呼び出しのための{root: map}を指定すると、その後、<map>はXMLのルートとなり、to_xml()がコールされたときにことを意味し各配列要素はメソッド{root: “map”}で呼び出され、各配列要素は<map>タグで折り返されます。例えば:

puts [{a: 10, b: 20}, {a: 100, b: 200}].to_xml({root: "map"}) 

--output:-- 

<?xml version="1.0" encoding="UTF-8"?> 
<map type="array"> 
    <map> 
    <a type="integer">10</a> 
    <b type="integer">20</b> 
    </map> 
    <map> 
    <a type="integer">100</a> 
    <b type="integer">200</b> 
    </map> 
</map> 

ネストされた<map>タグがto_xml()メソッドに組み込まれた機能の副作用です:例えば、アレイ上のto_xmlを呼び出すrootオプション():あなたは、複数の名前を指定した場合"マップ"すると、レールが回転して配列の各要素に対してto_xml()を呼び出すと、:rootオプションの単なる "マップ"が指定されます。配列に対してto_xml()を呼び出し、:rootオプションを "マップ"するように指定すると、自然に各配列要素が "マップ"になる可能性があります。もちろん、それはあなたが望むものではありません。

mr_sudacaが指摘したように幸運なことに、これがあります:ルートの子供のためのノードのデフォルト名によって

は root.singularizeです。 :children optionで変更できます。その結果、
http://apidock.com/rails/Array/to_xml

、このコード:

require 'ostruct' 

configs = [ 
    OpenStruct.new(
    key: "PNB_ALERTLOG_RECEIVER_CHANNEL", 
    value: "PNB_ALERTLOG_RECEIVER", 
    comment: "Canal que...", 
), 
    OpenStruct.new(
    key: "PNB_CFG_FILE_ACCESS_CONTROL", 
    value: "resources/configPnbDev/pnbAccessControl.json", 
    comment: "Este ficheiro...", 
) 
] 

entries = [] 

configs.each do |config| 
    entry = {} 
    conf = {} 

    entry.store('string', config.key) 

    conf.store('value', config.value) 
    conf.store('comment', config.comment) 

    entry.store('com.mirth.connect.util.ConfigurationProperty', conf) 

    entries << entry 
end 

p entries 
puts entries.to_xml(:root => 'map', children: "entry", :skip_types => true) 

は出力を生成します。

<?xml version="1.0" encoding="UTF-8"?> 
<map> 
    <entry> 
    <string>PNB_ALERTLOG_RECEIVER_CHANNEL</string> 
    <com.mirth.connect.util.ConfigurationProperty> 
     <value>PNB_ALERTLOG_RECEIVER</value> 
     <comment>Canal que...</comment> 
    </com.mirth.connect.util.ConfigurationProperty> 
    </entry> 
    <entry> 
    <string>PNB_CFG_FILE_ACCESS_CONTROL</string> 
    <com.mirth.connect.util.ConfigurationProperty> 
     <value>resources/configPnbDev/pnbAccessControl.json</value> 
     <comment>Este ficheiro...</comment> 
    </com.mirth.connect.util.ConfigurationProperty> 
    </entry> 
</map> 

あなたはまた、いくつかのあなたのエントリで問題とconfのハッシュを持っているようですが、私には見えますentries配列内のすべての要素は同じエントリとconfハッシュを参照するため、ループがこれらのハッシュを変更し続けるため、配列の各エントリはハッシュth atには、ループ内で最後に設定されたキー/値が格納されます。

+0

啓発と詳細説明@ 7studをありがとう、それは本当にto_xml()を理解するのに役立ちました。御時間ありがとうございます! – Ayanami

関連する問題