ruby to_xml - 删除对象节点

发布于 2024-11-08 17:00:45 字数 1562 浏览 0 评论 0原文

我正在解析活动记录中带有“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琉璃繁缕 2024-11-15 17:00:45

很遗憾没有人回答您的问题!我会尝试一下,尽管我不完全确定我遵循它是什么。

如果我理解正确,您应该能够通过执行以下操作来获得您想要的内容:

arrayHash.to_xml(:root => "Cenarios", :skip_types => true, :skip_instruct => true)

请注意,我将根更改为 Cenarios 复数。这将为您提供如下 XML:

<Cenorios>
  <Cenorio>
    <TamanhoRegistro>
      <nomeCampo>Tamanho Registro</nomeCampo>
      <X>10</X>
      <Y>20</Y>
      <valor>10</valor>
    </TamanhoRegistro>
    <VersaoRegistro>
      <nomeCampo>Versao Registro</nomeCampo>
      <X>10</X>
      <Y>20</Y>
      <valor>10</valor>
    </VersaoRegistro>
  </Cenorio>
</Cenorios>

或者您是说您希望将 TamanhoRegistroVersaoRegistro 等更改为 Cenario?如果是这样,假设你的 arrayHash 是这样的:

arrayHash = [hashExemplo, otherHash]

你可以这样做:

arrayHash.map(&:values).flatten.to_xml(:root => "Cenorios", :skip_types => true)

这会给你如下的 XML:

<Cenorios>
  <Cenorio>
    <nomeCampo>Tamanho Registro</nomeCampo>
    <X>10</X>
    <Y>20</Y>
    <valor>10</valor>
  </Cenorio>
  <Cenorio>
    <nomeCampo>Versao Registro</nomeCampo>
    <X>10</X>
    <Y>20</Y>
    <valor>10</valor>
  </Cenorio>
  <Cenorio>
    <nomeCampo>Tamanho Registro</nomeCampo>
    <X>10</X>
    <Y>20</Y>
    <valor>10</valor>
  </Cenorio>
  <Cenorio>
    <nomeCampo>Versao Registro</nomeCampo>
    <X>10</X>
    <Y>20</Y>
    <valor>10</valor>
  </Cenorio>
</Cenorios>

本质上,#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:

arrayHash.to_xml(:root => "Cenarios", :skip_types => true, :skip_instruct => true)

Note that I changed the root to Cenarios plural. This will give you XML like this:

<Cenorios>
  <Cenorio>
    <TamanhoRegistro>
      <nomeCampo>Tamanho Registro</nomeCampo>
      <X>10</X>
      <Y>20</Y>
      <valor>10</valor>
    </TamanhoRegistro>
    <VersaoRegistro>
      <nomeCampo>Versao Registro</nomeCampo>
      <X>10</X>
      <Y>20</Y>
      <valor>10</valor>
    </VersaoRegistro>
  </Cenorio>
</Cenorios>

Or were you saying that you want TamanhoRegistro and VersaoRegistro and such to be changed to Cenario? If so, assuming your arrayHash is something like this:

arrayHash = [hashExemplo, otherHash]

you could do something like this:

arrayHash.map(&:values).flatten.to_xml(:root => "Cenorios", :skip_types => true)

which would give you XML like the following:

<Cenorios>
  <Cenorio>
    <nomeCampo>Tamanho Registro</nomeCampo>
    <X>10</X>
    <Y>20</Y>
    <valor>10</valor>
  </Cenorio>
  <Cenorio>
    <nomeCampo>Versao Registro</nomeCampo>
    <X>10</X>
    <Y>20</Y>
    <valor>10</valor>
  </Cenorio>
  <Cenorio>
    <nomeCampo>Tamanho Registro</nomeCampo>
    <X>10</X>
    <Y>20</Y>
    <valor>10</valor>
  </Cenorio>
  <Cenorio>
    <nomeCampo>Versao Registro</nomeCampo>
    <X>10</X>
    <Y>20</Y>
    <valor>10</valor>
  </Cenorio>
</Cenorios>

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?

So尛奶瓶 2024-11-15 17:00:45

开始使用数组哈希,而不是哈希数组。
效果更好,唯一的问题是子根是哈希键及其烦人的东西。但没有人回答我,所以……我想我将不得不忍受这一点……

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文