2016-11-30 49 views
0

我有一个数据库对象数组,@configs,我想转换为XML格式但输出不是预期的。每个条目都包含在<map>标记中,而不是<entry>标记,我只希望<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> 

回答

1

设项如下哈希:

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

如果你写:

entries = [] 
entries << entry 
p entries 

那么输出是:

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

所以,如果你再写:

p entries.to_xml 

你认为单词“entry”会出现在输出中吗?这就像是期望输出:

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()时根选项,例如“maps”,然后当rails转向并在数组的每个元素上调用to_xml()时,rails将为:root选项指定单数“映射”。这是有道理的,因为如果你在数组上调用to_xml(),并且指定:root选项为“maps”,那么每个数组元素自然可能会是一个“map”。当然,那不是你想要的。

幸运的是,作为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哈希,并且由于您的循环不断更改这些哈希,数组中的每个条目都会引用哈希值at包含循环中设置的最后一个键/值。

+0

非常感谢您的启发和详细的解释@ 7stud,它真的帮助我了解to_xml()。谢谢你的时间! – Ayanami

相关问题