[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] XML::Twig demo



Somebody asked me for a demo like this (irc.perl.org #perl-help) today.
 
j
 
 
 
$ cat j.pl
#!/usr/bin/perl

use strict;
use XML::Twig;
my $twig = XML::Twig->new(
   pretty_print => 'indented'
)->parse('<users/>')->root;


my @users = (
  { firstname => 'Jay',
    lastname => 'Hannah'
  },
  { firstname => 'Quentus',
    lastname => 'Rex'
  },
);

foreach (@users) {
   my $elt = XML::Twig::Elt->new(
      user => {
         firstname => $_->{firstname},
         lastname  => $_->{lastname}
      }
   );
   $elt->paste( last_child => $twig );
}
print $twig->sprint;


$ perl j.pl
<users>
  <user firstname="Jay" lastname="Hannah"/>
  <user firstname="Quentus" lastname="Rex"/>
</users>