Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: adding XML records using XML::Twig

by toolic (Bishop)
on Aug 25, 2010 at 20:32 UTC ( [id://857290]=note: print w/replies, xml ) Need Help??


in reply to adding XML records using XML::Twig

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://857290]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-26 06:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found