Logo

Programming-Idioms

  • Java
  • Ruby
  • Haskell
  • JS
  • Perl

Idiom #104 Save object into XML file

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

use XML::LibXML qw();
$x->toFile('data.xml');
# gem install xml-mapping
require 'xml/mapping'
class Person
  include XML::Mapping
  attr_accessor :name, :surname, :age, :children

  text_node :from, "@from"
  text_node :name, "Name"
  text_node :surname, "Surname"

  def initialize(name, surname, ..)
    # ...
  end
end

x = Person.new(..)
x.save_to_xml.write($stdout,2) # nicely prints
x.save_to_file('data.xml')

need to create the schema in the class.
import orange.serialization._;
import orange.serialization.archives._;
import std.file;
auto archive = new XmlArchive!(char);
auto serializer = new Serializer(archive); 
serializer.serialize(x);
write("data.xml", archive.data);

Using the Orange serializer

New implementation...
< >
programming-idioms.org