balajinagaraju has asked for the wisdom of the Perl Monks concerning the following question:
Hi, Finally after having some tough time with XML parsing i am relatively comfortable now , but stuck with a small problem in parsing the below XML
<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>
I am using XML::Parser module to traverse through the elements but i am not able to define handlers for the attributes for this xml. How can i access the attributes in each step, here is my below code. I want to access each and every attribute like 'command' , 'param1'. etc. Any help is highly appreciated.
use warnings; use 5.010; use XML::Simple; use XML::MyXML; use XML::Twig; 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"; last SWITCH; } if ($element eq "step") { print "Step: "; $tag = "step"; last SWITCH; } if (%attrs eq "command") { print "Command: "; $tag = "Command"; last SWITCH; } } } sub endElement { my( $parseinst, $element ) = @_; if ($element eq "testcase") { print "\n\n"; } elsif ($element eq "title") { print "\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parse handler for Attributes in XML
by tangent (Parson) on Apr 10, 2012 at 10:13 UTC | |
by balajinagaraju (Sexton) on Apr 10, 2012 at 10:24 UTC | |
|
Re: Parse handler for Attributes in XML
by kcott (Archbishop) on Apr 10, 2012 at 10:09 UTC | |
by balajinagaraju (Sexton) on Apr 10, 2012 at 10:13 UTC | |
by kcott (Archbishop) on Apr 10, 2012 at 10:53 UTC |