Logo

Programming-Idioms

History of Idiom 104 > diff from v15 to v16

Edit summary for version 16 by ricc:
[Ruby] previous content was broken

Version 15

2022-04-19, 14:20:08

Version 16

2022-04-19, 14:47:49

Idiom #104 Save object into XML file

Write the contents of the object x into the file data.xml.

Idiom #104 Save object into XML file

Write the contents of the object x into the file data.xml.

Variables
x,data,xml
Variables
x,data,xml
Imports
Imports
# gem install xml-mapping
require 'xml/mapping'
Code
# TODO(ricc): finish this

class Person
  attr_accessor :name, :surname, :age
end
Code

class Person
  include XML::Mapping

  attr_accessor :name, :surname, :age, :children

  # single nodes
  text_node :from, "@from" # ,:default_value=>"My friend" migth works as well
  text_node :name, "Name"
  text_node :surname, "Surname"
  text_node :age, "Age"
  text_node :type, "Type", :default_value=> "human"
  # one array node
  array_node :children, "children", "ChildrenName", :class=>String, :default_value=>[]

  def initialize(name, surname, age, children=[])
    # add this to the init
Doc URL
https://stackoverflow.com/questions/58662688/xml-serialization-from-class-instance-in-ruby-with-custom-attributes
Doc URL
https://stackoverflow.com/questions/58662688/xml-serialization-from-class-instance-in-ruby-with-custom-attributes
Origin
https://multi-io.github.io/xml-mapping/#label-Example
Origin
https://multi-io.github.io/xml-mapping/#label-Example
Demo URL
https://multi-io.github.io/xml-mapping/#label-Example
Demo URL
https://multi-io.github.io/xml-mapping/#label-Example