[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Omaha.pm] XML::Twig demo #2
Later he decided he wanted elements instead of attributes. :)
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 my $user (@users) {
my $user_xml = XML::Twig::Elt->new('user');
foreach my $att (keys %$user) {
my $xml = XML::Twig::Elt->new($att, $user->{$att});
$xml->paste( last_child => $user_xml );
}
$user_xml->paste( last_child => $twig );
}
print $twig->sprint;
$ perl j.pl
<users>
<user>
<firstname>Jay</firstname>
<lastname>Hannah</lastname>
</user>
<user>
<firstname>Quentus</firstname>
<lastname>Rex</lastname>
</user>
</users>