XML::Twig





examples/xml/twig.pl
#!/usr/bin/perl
use strict;
use warnings;

use XML::Twig;
my $twig = XML::Twig->new(
    pretty_print => 'indented',
);
$twig->parsefile('examples/data.xml');

# print the content again (look at the pretty_print option)
# $twig->print;


my $root = $twig->root;
# print $root->name, "\n";        # data
# print $root->gi, "\n";          # data


#print $twig, "\n";               # HASH(0x.....)
#print $root->twig, "\n";         # HASH)0x.....)

#my $elem = $root->first_child;
#print $elem->gi, "\n";           # country
#print $elem->att('id'), "\n";    # 1

# replace the name of the root element by a new name
#$root->set_tag('new data');
#$twig->print;

examples/xml/twig1.pl
use strict;
use warnings;

my $t= XML::Twig->new();
  $t->parse( '<d><title>title</title><para>p 1</para><para>p 2</para></d>');
  my $root= $t->root;
  $root->set_tag( 'html');              # change doc to html
  $title= $root->first_child( 'title'); # get the title
  $title->set_tag( 'h1');               # turn it into h1
  my @para= $root->children( 'para');   # get the para children
  foreach my $para (@para)
    { $para->set_tag( 'p'); }           # turn them into p
  $t->print;                            # output the document