我有一個數據庫對象數組,@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>
非常感謝你@mr_sudaca,它的工作! :) – Ayanami