Dude see Re^3: parse xml and store data in array of hashesh its much simpler than that

#!/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=964 +274;part=1 my $xml = <<'__XML__'; <doc> <testcase1> <step1 command ="Launchapp " param1 = "executablepath" param2 = "d +irpath"/> <step2 command = "Dothis"/> <step3 command = "Changemode" param1 = "100"/> <step4 command = "CaptureRectanlge" param1 = "3" param2 = "96" par +am3 = "726" param4 = "580"/> <step5 command = "CompareImage" image1 = "D:\\img1.jpg" image2 = " +D:\\img2.jpg "/> </testcase1> <testcase2> <step1 command ="Launchapp " param1 = "executablepath" param2 = "d +irpath"/> <step2 command = "Dothis"/> <step3 command = "Changemode" param1 = "100"/> <step4 command = "CaptureRectanlge" param1 = "3" param2 = "96" par +am3 = "726" param4 = "580"/> <step5 command = "CompareImage" image1 = "D:\\img1.jpg" image2 = " +D:\\img2.jpg "/> </testcase2> </doc> __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' } } ];

In reply to Re^6: parse xml and trigger function modules. by Anonymous Monk
in thread parse xml and trigger function modules. by balajinagaraju

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.