ROXML XML MAPPING文本节点值

时间:2013-05-25 15:00:51

标签: ruby

我有一个简单的XML格式定义 <list> <tag postion="11" length="12">VALUE_NODE</tag> <other tag1,tag2 nodes...> </list>

我有以下课程:

Class List
 include ROXML 
 xml_accessor :tags, :as => [Tag] 
end

class Tag
 include ROXML

 xml_accessor :position, :from => "@position"
 xml_accessor :length, :from => "@length"
 end

 tag1 = Tag.new
 tag1.position = "2"
 tag1.length = "23"
 tag2 = Tag.new
 tag2.position = "2"
 tag2.length = "23"

 tag_list = TagList.new
 tag_list.tags = [tag1,tag2]

好的我可以使用setter / getter方法设置所有属性并序列化对象,但我如何设置value_node?喜欢 我搜索了很多页但没有成功。

1 个答案:

答案 0 :(得分:0)

我不知道这个宝石但是在github的项目中我读了https://github.com/Empact/roxml/blob/master/README.rdoc

xml_reader :value, :from => :content

所以你必须:

  class Tag
    include ROXML

    xml_accessor :position, :from => "@position"
    xml_accessor :length, :from => "@length"
    xml_accessor :my_node, :from => :content
  end

  tag1 = Tag.new
  tag1.position = "2"
  tag1.length = "23"
  tag1.my_node = "my Node value"

  tag_list = TagList.new
  tag_list.tags = [tag1]

所以

p tag_list.to_xml.to_s

给出

<taglist>
  <tag position="2" length="23">my Node value</tag>
</taglist>