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.


In reply to Re: Module for XML output by ambrus
in thread Module for XML output by rebugger

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.