Ruby on rails 如何在Rails中将哈希数组转换为XML?

Ruby on rails 如何在Rails中将哈希数组转换为XML?,ruby-on-rails,ruby,xml,Ruby On Rails,Ruby,Xml,我有一个数据库对象数组,@configs,我想将其转换为XML格式,但输出不是预期的。每个条目都包含在标记中,而不是标记中,我只希望是XML根。如何使用根目录构建XML,并将所有条目放在标记中? 提前感谢您的帮助和时间 这是我的密码: entries = Array.new entry = Hash.new conf = Hash.new @configs.each do |config| entry.store('string', confi

我有一个数据库对象数组,
@configs
,我想将其转换为XML格式,但输出不是预期的。每个条目都包含在
标记中,而不是
标记中,我只希望
是XML根。如何使用
根目录构建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)
entries=Array.new
entry=Hash.new
conf=Hash.new
@配置。每个do |配置|
entry.store('string',config.key)
conf.store('value',config.value)
conf.store('comment',config.comment)
entry.store('com.mirth.connect.util.ConfigurationProperty',conf)
条目“映射”,:缩进=>0,:跳过\u类型=>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>

PNB_警报日志_接收器_通道
PNB_警报日志_接收器
Canal que irÃreceber tudo que记录的com警告e错误
PNB\u CFG\u文件\u访问\u控制
resources/configPnbDev/pnbAccessControl.json
许可证配置器AUTECITAçÃe CONTROL o de Acessos。
PNB\u CFG\u文件\u连接\u池
resources/configPnbDev/pnbConnectionPools.json
配置Oracle通用连接池usadas pelo PNB(PEM,RCU2)
PNB\U CFG\U文件\U CSP\U MC\U排除
resources/configPnbDev/medCronExclusions/mcexcurrentrevision.json
不适用
PNB\u CFG\u文件\u设施\u别名
resources/configPnbDev/snsFacilitiesAlias.json
SNS研究所别名地图。
我想要的是:

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

PNB_警报日志_接收器_通道
PNB_警报日志_接收器
Canal que irÃreceber tudo que记录的com警告e错误
PNB\u CFG\u文件\u访问\u控制
资源/configPnbDev/pnbAccessControl.json
许可证配置器AUTECITAçÃe CONTROL o de Acessos。
PNB\u CFG\u文件\u连接\u池
resources/configPnbDev/pnbConnectionPools.json
配置Oracle通用连接池usadas pelo PNB(PEM,RCU2)
PNB\U CFG\U文件\U CSP\U MC\U排除
resources/configPnbDev/medCronExclusions/mcexcurrentrevision.json
不适用
PNB\u CFG\u文件\u设施\u别名
resources/configPnbDev/snsFacilitiesAlias.json
SNS研究所的别名入口。
试试这个:

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

试试这个:

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

来源:

假设条目是以下哈希:

entry = {
  a: “hello”,
  b: “goodbye”,
}
如果你写:

entries = []
entries << entry
p entries
因此,如果你接着写:

p entries.to_xml
您认为“条目”一词将如何出现在输出中?这有点像期望的输出:

x = 10
y = 20
puts x+y
把字母“x”和“y”放在某处

根据数组的to_xml()文档:

返回一个字符串。。。通过对每个元素调用xml。
选项哈希向下传递。

选项散列向下传递的事实意味着,当您为数组上的to_xml()调用指定
{root:map}
时,
将成为xml的根,当对每个数组元素调用
to_xml()
时,将使用
{root: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>
生成输出:

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

PNB_警报日志_接收器_通道
PNB_警报日志_接收器
运河。。。
PNB\u CFG\u文件\u访问\u控制
resources/configPnbDev/pnbAccessControl.json
埃斯特菲切罗。。。

在我看来,您的entry和conf散列也有一些问题,因为entries数组中的每个元素都引用相同的entry和conf散列,并且由于循环不断更改这些散列,数组中的每个条目都将引用一个散列,其中包含循环中设置的最后一个键/值。

假设条目是以下散列:

entry = {
  a: “hello”,
  b: “goodbye”,
}
如果你写:

entries = []
entries << entry
p entries
因此,如果你接着写:

p entries.to_xml
您认为“条目”一词将如何出现在输出中?这有点像期望的输出:

x = 10
y = 20
puts x+y
把字母“x”和“y”放在某处

根据数组的to_xml()文档:

返回一个字符串。。。通过对每个元素调用xml。
选项哈希向下传递。

选项散列向下传递的事实意味着,当您为数组上的to_xml()调用指定
{root:map}
时,
将成为xml的根,当对每个数组元素调用
to_xml()
时,将使用
{root: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>
生成输出:

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

PNB_警报日志_接收器_通道
PNB_警报日志_接收器
运河。。。
PNB\u CFG\u文件\u访问\u控制
resources/configPnbDev/pnbAccessControl.json
埃斯特菲切罗。。。

在我看来,您的entry和conf散列也有一些问题,因为entries数组中的每个元素都引用相同的entry和conf散列,并且由于循环不断更改这些散列,数组中的每个条目都将引用一个哈希,其中包含循环中设置的最后一个键/值。

非常感谢您的启发和详细解释@7stud,它确实帮助我理解了\u xml()。谢谢你的时间!非常感谢您的启发和详细解释