使用带有acts_as_api 的属性格式化xml [英] Format xml with properties with acts_as_api
问题描述
我对acts_as_api 有疑问.我想像这样格式化一个 xml 字符串:
i have a doubt with acts_as_api. i want to format a xml string like this:
<root>
<child property="property_value">Some Text</child>
etc
</root>
这可能吗?
提前致谢
推荐答案
我认为这不会自动工作,因为您想将数据放在 XML 结构中的两个不同位置:
I think this will not work automagically as you want to put data in your XML structure in two different places:
- 节点的属性
- 节点的内容
但是您的模型的属性包含单个哈希.acts_as_api
(或其他序列化程序)无法确定哪些属性应该在属性中,哪些应该作为内部文本.
But the attributes of your model are contained a single Hash.
acts_as_api
(or another serializer) can't determine which attributes are supposed to be in a property and which one should be taken as the inner text.
对于这种情况,您必须提供自己的序列化程序(您可以查看 http://api.rubyonrails.org/classes/ActiveRecord/Serialization.html#method-i-to_xml 关于如何在 Rails 中执行此操作.
For this case you will have to provide your own serializer (you can have a look at http://api.rubyonrails.org/classes/ActiveRecord/Serialization.html#method-i-to_xml on how to do it in Rails.
这篇关于使用带有acts_as_api 的属性格式化xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!