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'
}
}
];
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.