XML-Twig - reading and writing into xml

Posted on Fri Sep 1 17:14:21 2006 by aloknath251
reading and writing into xml
Hi, I have this xml which I want to process.Basically I want to do this two operations 1. Print the students name for a given class. 2. Insert a student in a given class. It just became complicated after I added few levels. Can anybody guide me please ? Thanx, Alok.
#!/usr/bin/perl -w use strict ; use XML::Twig; my $twig = XML::Twig->new(pretty_print => 'indented'); $twig->parsefile('testTwig.xml'); my $root = $twig->root ; my $className ; # display students of a class foreach my $element ($twig->root->children('classes')) { $className = $element->{'att'}->{"name"} ; #print the students of 'primary' class if ($className eq "Primary") { print "students for this class : \n"; } } # insert student in a class my $node = XML::Twig::Elt->new( 'User', {'name' => "Johny"}, XML::Twig::Elt->new( 'age' => 25 )) ; ??? Where to paste this $twig->print;
contents of testTwig.xml
< school >

< classes name="Primary" >
< student name="Junkman" >
< Age > 12 < /Age >
< /student >

< student name="Lotman" >
< Age > 14 < /Age >
< /student >
< /classes >

< classes name="Nursery" >
< student name="Testman" >
< Age > 34 < /Age >
< /student >
< /classes >

< classes name="SomeClass" >
< /classes >
< /school >
Direct Responses: 2889 | Write a response