#!/usr/bin/perl -- #~ 2011-07-23-08:42:20+0000 by Anonymous Monk #~ 2012-04-10-00:42:57 updated for GrandFather/balajinagaraju #~ perltidy -csc -otr -opr -ce -nibc -i=4 use strict; use warnings; use autodie; # dies if open/close... fail use Data::Dumper; use XML::Twig; Main( @ARGV ); exit( 0 ); sub Main { Demo(); } sub NotDemoMeaningfulName { my ($xml) = @_; my @data; my %testcase; my $t = XML::Twig->new( twig_handlers => { '/doc/*' => sub { warn $_->path; push @data, { %testcase } if %testcase; %testcase = (); $_->purge; # free memory }, '/doc/*/*' => sub { warn $_->path; $testcase{ $_->tag } = $_->atts ; }, }, ); $t->xparse($xml); $t->purge; return \@data; } ## end sub NotDemoMeaningfulName sub Demo { my $ref = NotDemoMeaningfulName( DemoData() ); print Dumper($ref); } sub DemoData { #~ http://perlmonks.com/?abspart=1;displaytype=displaycode;node_id=964274;part=1 my $xml = <<'__XML__'; __XML__ return $xml; } ## end sub DemoData __END__ $ perl xml.twig.964274.pl /doc/testcase1/step1 at xml.twig.964274.pl line 31. /doc/testcase1/step2 at xml.twig.964274.pl line 31. /doc/testcase1/step3 at xml.twig.964274.pl line 31. /doc/testcase1/step4 at xml.twig.964274.pl line 31. /doc/testcase1/step5 at xml.twig.964274.pl line 31. /doc/testcase1 at xml.twig.964274.pl line 25. /doc/testcase2/step1 at xml.twig.964274.pl line 31. /doc/testcase2/step2 at xml.twig.964274.pl line 31. /doc/testcase2/step3 at xml.twig.964274.pl line 31. /doc/testcase2/step4 at xml.twig.964274.pl line 31. /doc/testcase2/step5 at xml.twig.964274.pl line 31. /doc/testcase2 at xml.twig.964274.pl line 25. $VAR1 = [ { 'step2' => { 'command' => 'Dothis' }, 'step1' => { 'param2' => 'dirpath', 'param1' => 'executablepath', 'command' => 'Launchapp ' }, 'step4' => { 'param4' => '580', 'param2' => '96', 'param3' => '726', 'param1' => '3', 'command' => 'CaptureRectanlge' }, 'step5' => { 'image1' => 'D:\\\\img1.jpg', 'image2' => 'D:\\\\img2.jpg ', 'command' => 'CompareImage' }, 'step3' => { 'param1' => '100', 'command' => 'Changemode' } }, { 'step2' => { 'command' => 'Dothis' }, 'step1' => { 'param2' => 'dirpath', 'param1' => 'executablepath', 'command' => 'Launchapp ' }, 'step4' => { 'param4' => '580', 'param2' => '96', 'param3' => '726', 'param1' => '3', 'command' => 'CaptureRectanlge' }, 'step5' => { 'image1' => 'D:\\\\img1.jpg', 'image2' => 'D:\\\\img2.jpg ', 'command' => 'CompareImage' }, 'step3' => { 'param1' => '100', 'command' => 'Changemode' } } ];