Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Using function modules and passing arguments by parsing an XML.

by balajinagaraju (Sexton)
on Apr 11, 2012 at 10:50 UTC ( #964498=perlquestion: print w/ replies, xml ) Need Help??
balajinagaraju has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am stuck at a situation in my task and want your help in solving it. A brief background about the problem statement is as below. I am parsing the Below xml using the XML::parser module and i am able to parse the XML. My XML and the code i am using to parse is as below. My script should perform the below operations. 1)Access each testcase defined in the XML and perform the operations mentioned in each step. For each step there is a command and parameters for the command. This command is basically a perl function module which i have written and its placed in a seperate perl file. 2)My script should access each step and trigger the corresponding function with the specified parameter. The parameters are specified as Arguments in my XML, Uptil now i was only able to parse the XML using the below code but i am not able to trigger functions based on the steps, since the output of the parser is kind a confusing and i don't know how i can use this output. Please suggest how i can proceed, if you have any doubts in regarding the problems please let me know.

<DCA> <testcase type = "Interactive"> <step command ="LaunchApp " param1 = "executablepath" param2 = "di +rpath"/> <step command = "Gohere"/> <step command = "Changemode" param1 = "100"/> <step command = "CaptureRectanlge" param1 = "3" param2 = "96" para +m3 = "726" param4 = "580"/> <step command = "CompareImage" param1 = "D:\\img1.jpg" param2 = "D +:\\img2.jpg "/> </testcase> <testcase type = "broswer"> <step command ="Launchapp " param1 = "executablepath" param2 = "di +rpath"/> <step command = "Gohere"/> <step command = "Channgemode" param1 = "100"/> <step command = "CaptureRectanlge" param1 = "3" param2 = "96" para +m3 = "726" param4 = "580"/> <step command = "CompareImage" image1 = "D:\\img1.jpg" image2 = "D +:\\img2.jpg "/> </testcase> </DCA> MyCode: use warnings; use 5.010; use XML::Simple; #using XML::Parser use XML::Parser; my $parser = new XML::Parser; $parser->setHandlers(Start => \&startElement, End => \&endElement); + $parser->parsefile('D:\\ABC\\Perl Projects\\DCA.xml'); sub startElement { my( $parseinst, $element, %attrs ) = @_; SWITCH: { if ($element eq "testcase") { $count++; $tag = "testcase"; print "Testcase $count:\n"; for my $attr (keys %attrs) { print "$attr: $attrs{$attr}\n"; } last SWITCH; } if ($element eq "step") { print "Step: "; $tag = "step"; for my $attr (keys %attrs) { print "$attr: $attrs{$attr}\n"; } last SWITCH; } } } sub endElement { my( $parseinst, $element ) = @_; if ($element eq "testcase") { print "\n\n"; } elsif ($element eq "title") { print "\n"; } } Output of my script Testcase 1: type: Interactive Step: param2: dirpath param1: executablepath command: Launchsimulator Step: command: Homescreen Step: param1: 100 command: Channelchange Step: param4: 580 param2: 96 param3: 726 param1: 3 command: CaptureRectanlge Step: param2: D:\\img2.jpg param1: D:\\img1.jpg command: CompareImage Testcase 2: type: Netfront Step: param2: dirpath param1: executablepath command: Launchsimulator Step: command: Homescreen Step: param1: 100 command: Channelchange Step: param4: 580 param2: 96 param3: 726 param1: 3 command: CaptureRectanlge Step: image1: D:\\img1.jpg image2: D:\\img2.jpg command: CompareImage Testcase 3: type: HTML

So if you see the output i am getting tiggers for each of my registered handlers but i am not able to make use of the output for my scenario. Any suggestion is highly appreciated. If you have better design idea also please provide your suggestions.

Comment on Using function modules and passing arguments by parsing an XML.
Download Code
Re: Using function modules and passing arguments by parsing an XML.
by Anonymous Monk on Apr 11, 2012 at 11:44 UTC
      Hi , Sorry i somehow missed that reply of your's, thanks for your timely help i will try your solution. Thanks for your time.
Re: Using function modules and passing arguments by parsing an XML.
by tangent (Curate) on Apr 11, 2012 at 11:46 UTC
    I'm not quite sure of your requirement but this may help:
    sub startElement { my( $parseinst, $element, %attrs ) = @_; SWITCH: { # ... if ($element eq "step") { my $command = $attrs{command}; $command =~ s/\s+//g; # get rid of spaces if ($command eq 'LaunchApp') { MyModule::LaunchApp(%attrs); } elsif ($command eq 'Changemode') { MyModule::Changemode(%attrs); } # and so on for each command last SWITCH; } } } # This is your functions module package MyModule; sub LaunchApp { my %attrs = @_; print "\n----\nIn Sub MyModule::LaunchApp\n"; for my $attr (keys %attrs) { print "$attr: $attrs{$attr}\n"; } print "Leaving Sub MyModule::LaunchApp\n----\n"; } sub Changemode { my %attrs = @_; print "\n----\nIn Sub MyModule::Changemode\n"; for my $attr (keys %attrs) { print "$attr: $attrs{$attr}\n"; } print "Leaving Sub MyModule::Changemode\n----\n"; } 1;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://964498]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-12-28 00:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls