我有一个数据库对象数组,@configs,我想要转换成XML格式,但是输出不是预期的。每个条目都包含在一个<map>标记中,而不是一个<entry>标记中,我只希望<tag>成为XML。如何使用<tag>根目录构建XML并将所有条目放入<entry>标记中?提前感谢您的帮助和时间!
这是我的代码:
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>发布于 2016-11-30 18:40:35
试试这个:
pp entries.to_xml(:root => 'map', :children => 'entry', :indent => 0, :skip_types => true)来源:xml
发布于 2016-12-01 04:11:13
假设条目是以下哈希:
entry = {
a: “hello”,
b: “goodbye”,
}如果你写:
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。 选项散列向下传递。 xml
选项散列向下传递的事实意味着,当您为数组上的{root: map} ()调用指定to_xml时,<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()时指定了一个复数名称,例如“to_xml()”,那么当rails转身并对数组的每个元素调用to_xml()时,rails将为:root选项指定单数“map”。这是有意义的,因为如果在数组上调用to_xml()并指定:root选项为"map“,那么每个数组元素很可能是”map“。当然这不是你想要的。
幸运的是,正如mr_sudaca所指出的,有这样的情况:
在默认情况下,根节点子节点的名称是root.singularize。您可以使用
:children option来更改它。 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散列也有一些问题,因为条目数组中的每个元素都将引用相同的条目和conf哈希,而且由于您的循环不断地更改这些散列,数组中的每个条目都将引用包含循环中设置的最后一个键/值的散列。
https://stackoverflow.com/questions/40895141
复制相似问题