http://qs321.pair.com?node_id=857284

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all fairly new to Perl and are playing with some XML files but cant work out using XML::Twig on how to add extra entries... My XML file looks like this
<sites> <site siteid="ONE"> <name>name1</name> <address>address1</address> <contact>contact1</contact> </site> <site siteid="TWO"> <name>name2</name> <address>address2</address> <contact>contact2</contact> </site> </sites>
I have code which can successfully read relevant items from the XML
use XML::Twig; $sitefile='/app/sites.xml'; $twig=new XML::Twig; $twig -> parsefile($sitefile); $root=$twig->root; @sites=$root->children; foreach $site (@sites){ print "Site ".$site->att("siteid")."\n"; #$address = $site->first_child("address")->text; #print $address."\n"; }
but cant work out how i'd add more "sites" and their relevant children using XML::TWIG
any help appreciated thanks A.

Replies are listed 'Best First'.
Re: adding XML records using XML::Twig
by toolic (Bishop) on Aug 25, 2010 at 20:32 UTC
    This should be a good start; it adds a 3rd site with an id attribute and name element. You should try to add the address/contact elements. See also the XML::Twig tutorial.
    use strict; use warnings; use XML::Twig; my $xml = q( <sites> <site siteid="ONE"> <name>name1</name> <address>address1</address> <contact>contact1</contact> </site> <site siteid="TWO"> <name>name2</name> <address>address2</address> <contact>contact2</contact> </site> </sites> ); my $twig = XML::Twig->new(); $twig->parse($xml); my $root=$twig->root; my $site = $root->first_child('site'); my $cp = $site->copy(); $cp->set_att('siteid', 'THREE'); $cp->first_child('name')->set_text('name3'); $cp->paste('last_child', $root); my @sites = $root->children; for my $site (@sites){ print "Site ", $site->att("siteid"), "\n"; print "name ", $site->first_child('name')->text(), "\n"; } __END__ Site ONE name name1 Site TWO name name2 Site THREE name name3

    Updated to use the copy method.

Re: adding XML records using XML::Twig
by murugu (Curate) on Aug 26, 2010 at 01:59 UTC
    #!/usr/bin/perl -w use strict; my $sitefile='/app/sites.xml'; my $twig=new XML::Twig; $twig -> parsefile($sitefile); my %hash = (name=>"name3",address=>"address3",contact=>"contact3"); my $root=$twig->root; my $insert = $root->insert_new_elt('last_child', site => {siteid=>"THR +EE"}); foreach my $element (keys %hash){ $insert->insert_new_elt('last_child',$element,$hash{$element}) } $root->print;

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

      How to insert short-form like: <description mydescription /> ? Thanks.
        If you pass text as the third argument to insert_new_elt() it will be bracketed by the given element tags. However, if you pass a hash reference, you can instead change the elements attributes. So:
        $insert->insert_new_elt('last_child','description', 'mydescription' ); # creates: <description>mydescription</description> $insert->insert_new_elt('last_child','description', { desc => 'mydescr +iption' } ); # creates: <description desc="mydescription" />
Re: adding XML records using XML::Twig
by ambrus (Abbot) on Jul 24, 2013 at 11:07 UTC

    See also Re: Module for XML output for two styles of adding records: building the new records starting from the leaves or from the parents.