You can use XML::Twig to create new XML files. There's not many example for this around, probably because XML::LibXML is for those who believe in XML, and XML::Twig is for those who don't but have to work with it, and obviously it's more frequently the former who want to create new XML. (Update 2011-01-13: on the other hand, code for building XML from scratch with XML::LibXML can look a bit ugly, because XML::LibXML is a straight wrapper over the C library, so it won't give you methods that accept a variable number and type of arguments like the constructors I'm using below.)
Anyway, there are two styles you can use to create new XML with Twig (though you can mix them). Here.
use warnings; use strict;
use XML::Twig;
{ # from the inside
my $para1text1 = "There are two ways to build XML with ";
my $moduletext = "Twig";
my $module = XML::Twig::Elt->new("a", {"href" => "http://mirod
+.org/"}, $moduletext);
my $para1text2 = ": ";
my $para1 = XML::Twig::Elt->new("p", $para1text1, $module, $p
+ara1text2);
my $para2text = "from the inside and from outside.";
my $para2 = XML::Twig::Elt->new("p", $para2text);
my $root = XML::Twig::Elt->new("saying", $para1, $para2);
my $twig = XML::Twig->new(pretty_print => "nice"); $twig->se
+t_root($root);
$twig->flush(*STDOUT);
}
{ # from the outside
my $twig = XML::Twig->new(pretty_print => "nice");
my $root = XML::Twig::Elt->new("saying"); $twig->set_root($root);
my $para1 = $root->insert_new_elt(last_child => "p");
$para1->suffix("There are two ways to build XML with ");
my $module = $para1->insert_new_elt(last_child => "a",
{"href" => "http://mirod.org/"});
# or this would work too:
#$module = $para1->insert_new_elt(last_child => "a");
#$module->set_att("href" => "http://mirod.org/");
$module->suffix("Twig");
$para1->suffix(": ");
my $para2 = $root->insert_new_elt(last_child => "p");
$para2->suffix("from the inside and from outside.");
$twig->flush(*STDOUT);
}
If you don't wish to keep the whole XML structure in memory, you have to use the second method for at least the outer layers. After adding each larger chunk of the document (here after adding each paragraph), you call the flush method on that element which both outputs the XML document up to that part, and removes it from the document tree so it's no longer in the memory. Just don't forget to flush the twig at the very end so that the last closing tags are output. For example,
{ # from the outside, flushing after each paragraph
my $twig = XML::Twig->new(pretty_print => "nice");
my $root = XML::Twig::Elt->new("saying"); $twig->set_root($root);
my $para1 = $root->insert_new_elt(last_child => "p");
$para1->suffix("There are two ways to build XML with ");
my $module = $para1->insert_new_elt(last_child => "a",
{"href" => "http://mirod.org/"});
$module->suffix("Twig");
$para1->suffix(": ");
$para1->flush(*STDOUT);
my $para2 = $root->insert_new_elt(last_child => "p");
$para2->suffix("from the inside and from outside.");
$para2->flush(*STDOUT);
$twig->flush(*STDOUT);
}
{ # combination, flusing after each paragraph
my $twig = XML::Twig->new(pretty_print => "nice");
my $root = XML::Twig::Elt->new("saying"); $twig->set_root($root);
my $para1text1 = "There are two ways to build XML with ";
my $moduletext = "Twig";
my $module = XML::Twig::Elt->new("a", {"href" => "http://mirod
+.org/"}, $moduletext);
my $para1text2 = ": ";
my $para1 = XML::Twig::Elt->new("p", $para1text1, $module, $p
+ara1text2);
$para1->paste($root);
$para1->flush;
my $para2text = "from the inside and from outside.";
my $para2 = XML::Twig::Elt->new("p", $para2text);
$para2->paste($root);
$para2->flush;
$twig->flush(*STDOUT);
}
Update 2013-10-21: see the later question Best module for Creating [Writing out] XML.