History of Idiom 104 > diff from v16 to v17
Edit summary for version 17 by ricc:
[Ruby] damn limitation!
[Ruby] damn limitation!
↷
Version 16
2022-04-19, 14:47:49
Version 17
2022-04-19, 14:50:20
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,xmlVariables
x,data,xmlCode
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
Code
class Person
include XML::Mapping
attr_accessor :name, :surname, :age, :children
text_node :from, "@from"
text_node :name, "Name"
text_node :surname, "Surname"
text_node :age, "Age"
array_node :children, "children", "ChildrenName", :class=>String, :default_value=>[]
def initialize(name, surname, age, children=[])
# ...
end
end
x = Person.new('John', 'Doe', 42, %w{Alice Bob})
x.save_to_xml.write($stdout,2)
x.save_to_file('data.xml')
Comments bubble