Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^4: parse xml and trigger function modules.

by balajinagaraju (Sexton)
on Apr 10, 2012 at 05:01 UTC ( [id://964250]=note: print w/replies, xml ) Need Help??


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

Hi, I tried to work with XML::Twig and was successful upto some extent, but still facing problems in accessing certain areas of my XML.How do i access the steps mentioned in each testcase and there corresponding attributes, the solution is quiet urgent for me, can you please suggest how i can access these elements and attributes.

use strict; use warnings; use 5.010; use XML::Simple; use XML::MyXML; use XML::Twig; my $twig= new XML::Twig( twig_handlers => { testcase => \&testcase }); # create the twig $twig->parsefile( 'D:\\ABC\\Perl Projects\\DCA.xml'); #$twig->print; my $root= $twig->root; #$root->set_tag( 'html'); $twig->print; my @testcase = $root->children('testcase'); # foreach my $testcase (@testcase) # { $testcase->set_tag('p'); } # turn them into p $twig->print; sub testcase{ my ($twig, $testcase) = @_; print $testcase->field('command6'); print $testcase->att('type'); print "\n"; }

Replies are listed 'Best First'.
Re^5: parse xml and trigger function modules.
by GrandFather (Saint) on Apr 10, 2012 at 07:09 UTC

    Something like:

    use strict; use warnings; use XML::Twig; my $xmlStr = <<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 my $twig = new XML::Twig(twig_handlers => {'*' => \&testcase}); $twig->parse($xmlStr); sub testcase { my ($twig, $testcase) = @_; my $tag = $testcase->tag(); return 1 if $tag !~ /^testcase\d+$/; print "$tag\n"; for my $step ($testcase->children()) { my $tag = $step->tag(); my %attrs = %{$step->atts()}; print "Command: $attrs{'command'}("; print join ', ', map {$attrs{$_}} grep {/^param/} sort keys %a +ttrs; print ")\n"; } print "\n"; }

    Prints:

    testcase1 Command: Launchapp (executablepath, dirpath) Command: Dothis() Command: Changemode(100) Command: CaptureRectanlge(3, 96, 726, 580) Command: CompareImage() testcase2 Command: Launchapp (executablepath, dirpath) Command: Dothis() Command: Changemode(100) Command: CaptureRectanlge(3, 96, 726, 580) Command: CompareImage()

    This is somewhat messier than it ought be because the test case tags are all different. If the tags were 'testcase' and the case identifier were an attribute the twig_handlers expression could be just 'testcase' and the testing in the handler sub is no longer required.

    True laziness is hard work

      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://964250]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-23 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found