0

我有一个简单的 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?就像我搜索了很多页面但没有成功。

4

1 回答 1

0

我不知道这个 gem,但是在 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>
于 2013-05-25T23:15:10.013 回答