开发者

Rails #to_xml don't add root elements

开发者 https://www.devze.com 2023-03-19 06:14 出处:网络
As far as I know, this is valid xml, if not the most elegant: <items> <item>...</item>

As far as I know, this is valid xml, if not the most elegant:

<items>
  <item>...</item>
  <item>...</item>
</items>

When parsing this (with HTTParty's built in Crack::XML) I get

{:items => {:item => [...]}}

Fine, but when I convert this back to xml in Rails it adds an element:

<items>
  <item>
    <item>...</item>
    <item>...</item>
  </item>
</items>

Is there an option, or more likely, a way to customize the builder's behavior to avoid this? I see a way to specify what this root element to_xml is adding is named, but not a way to make it assume that if the value of a hash pair is an array, to wrap each element of the array in an element with the name of the key, so that to_xml will return something with a similar structure of what I started with.

UPDATE

Perhaps a simpler way to ask this question: is there a parser/serializer combin开发者_JAVA技巧ation that is reversible, so that if I parse some xml and then serialize the resulting Hash, I will get the same XML?

0

精彩评论

暂无评论...
验证码 换一张
取 消