Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^6: parse xml and trigger function modules.

by Anonymous Monk
on Apr 10, 2012 at 07:39 UTC ( [id://964280]=note: print w/replies, xml ) Need Help??


in reply to Re^5: parse xml and trigger function modules.
in thread parse xml and trigger function modules.

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' } } ];

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-03-28 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found