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, $para1text2); 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->set_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); } #### { # 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, $para1text2); $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); }