http://qs1969.pair.com?node_id=964081


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

I tried it using XML::Simple but this gives me a hash reference which is basically a unsorted datastructure, i used XML::Parser as well but it went above my head.. :-). Now trying to use XML::Twig.This seems to be pretty handy but wanted to gather information on the best available and a easy way to do it.

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

    XML::Twig is frequently recommended and is well documented with lots of example code. Show us what you have tried and we will help correct your code. You will get much better help and learn a lot more that way than having us write a chunk of code for you.

    True laziness is hard work

      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"; }

        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
Re^3: parse xml and trigger function modules.
by Anonymous Monk on Apr 09, 2012 at 04:45 UTC