ruby to_xml - 删除对象节点
我正在解析活动记录中带有“to_xml”的哈希数组,我想知道是否可以删除一些节点 这是解析代码:
arrayHash.to_xml(:root => "Cenario", :skip_types => true, :skip_instruct => true)
我得到这样的 XML:
<Cenario>
<Cenario nil="true"></Cenario>
<Cenario>
<TamanhoRegistro>
<nomeCampo>Tamanho Registro</nomeCampo>
<X>10</X>
<Y>20</Y>
<valor>10</valor>
</TamanhoRegistro>
<VersaoRegistro>
<nomeCampo>Versao Registro</nomeCampo>
<X>20</X>
<Y>30</Y>
</VersaoRegistro>
</Cenario>
.
.
.
.
<Cenario>
<TamanhoRegistro>
<nomeCampo>Tamanho Registro</nomeCampo>
<X>10</X>
<Y>20</Y>
<valor>10</valor>
</TamanhoRegistro>
<VersaoRegistro>
<nomeCampo>Versao Registro</nomeCampo>
<X>20</X>
<Y>30</Y>
</VersaoRegistro>
</Cenario>
</Cenario>
这是填充每个数组槽的哈希值。
hashExemplo = {
"TamanhoRegistro" => {'nomeCampo' =>'Tamanho Registro', 'X'=> '10', 'Y'=> '20', 'valor' => '10'},
"VersaoRegistro" => {'nomeCampo' =>'Versao Registro', 'X'=> '10', 'Y'=> '20', 'valor' => '10'},
id => id}
我想删除该节点,将根更改为“Cenarios”,将每个对象(哈希)更改为“Cenario”。
顺便说一句,抱歉英语不好。尽我所能去改进它。
干杯。
Im parsing a array of hash's with 'to_xml' from active record and im wondering if i can remove some nodes
Here is the parsing code:
arrayHash.to_xml(:root => "Cenario", :skip_types => true, :skip_instruct => true)
i get a XML like that:
<Cenario>
<Cenario nil="true"></Cenario>
<Cenario>
<TamanhoRegistro>
<nomeCampo>Tamanho Registro</nomeCampo>
<X>10</X>
<Y>20</Y>
<valor>10</valor>
</TamanhoRegistro>
<VersaoRegistro>
<nomeCampo>Versao Registro</nomeCampo>
<X>20</X>
<Y>30</Y>
</VersaoRegistro>
</Cenario>
.
.
.
.
<Cenario>
<TamanhoRegistro>
<nomeCampo>Tamanho Registro</nomeCampo>
<X>10</X>
<Y>20</Y>
<valor>10</valor>
</TamanhoRegistro>
<VersaoRegistro>
<nomeCampo>Versao Registro</nomeCampo>
<X>20</X>
<Y>30</Y>
</VersaoRegistro>
</Cenario>
</Cenario>
Here is the hash that populates each array slot.
hashExemplo = {
"TamanhoRegistro" => {'nomeCampo' =>'Tamanho Registro', 'X'=> '10', 'Y'=> '20', 'valor' => '10'},
"VersaoRegistro" => {'nomeCampo' =>'Versao Registro', 'X'=> '10', 'Y'=> '20', 'valor' => '10'},
id => id}
I wanted to remove the node, change the root to 'Cenarios' and each object (hash) to 'Cenario'.
Btw, sorry for the poor english. Trying my best to improve it.
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很遗憾没有人回答您的问题!我会尝试一下,尽管我不完全确定我遵循它是什么。
如果我理解正确,您应该能够通过执行以下操作来获得您想要的内容:
请注意,我将根更改为
Cenarios
复数。这将为您提供如下 XML:或者您是说您希望将
TamanhoRegistro
和VersaoRegistro
等更改为Cenario
?如果是这样,假设你的 arrayHash 是这样的:你可以这样做:
这会给你如下的 XML:
本质上,#map 将迭代数组,从哈希值的每个哈希中获取一个数组,所以该数组现在将是一个数组的数组,因此 #flatten 将其展平为单个数组。
这能回答你的问题吗,还是我错过了什么?
Sorry to see no one answered your question! I'll give it a try, although I'm not totally sure I follow what it is.
If I understand you correctly, you should be able to get what you want by doing the following:
Note that I changed the root to
Cenarios
plural. This will give you XML like this:Or were you saying that you want
TamanhoRegistro
andVersaoRegistro
and such to be changed toCenario
? If so, assuming your arrayHash is something like this:you could do something like this:
which would give you XML like the following:
Essentially, #map will iterate over the array, getting an array from each hash of the values in the hash, so the array will now be an array of arrays, so #flatten flattens that into a single array.
Does that answer your question, or have I missed something?
开始使用数组哈希,而不是哈希数组。
效果更好,唯一的问题是子根是哈希键及其烦人的东西。但没有人回答我,所以……我想我将不得不忍受这一点……
Instead of array of hashs, started to use hashs of arrays.
Works better, only problem is the subroot is the hash key and its kinf of annoying. but noone answerd me so... guess i will have to live with that...