Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Parse handler for Attributes in XML

by balajinagaraju (Sexton)
on Apr 10, 2012 at 09:03 UTC ( #964291=perlquestion: print w/ replies, xml ) Need Help??
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"; } }

Comment on Parse handler for Attributes in XML
Select or Download Code
Re: Parse handler for Attributes in XML
by kcott (Abbot) on Apr 10, 2012 at 10:09 UTC

    I'm not particularly familiar with XML::Parser but this line in particular leapt out at me:

    if (%attrs eq "command") {

    I suspect you want something like:

    if (exists $attrs{command}) {

    I also noticed your XML has no closing tag (i.e. </DCA>) - possibly a cut-n-paste error.

    -- Ken

      Thanks Ken, But i want to access specific attributes rather than checking for the presence of the attributes.

        $attrs{command} is a specific attribute. Checking for existence before attempting to access a value (e.g. ... $attrs{command} eq 'some_value' ...) avoids autovivification; depending on what other code you write, this may or may not be important.

        However, the main point I was trying to get across was that %attrs eq "command" was not doing what you wanted/intended/expected. The following short piece of code may help to further explain this:

        $ perl -Mstrict -Mwarnings -E ' > my %x = (a => 1); > say +(%x eq q{a}) ? 1 : 0; > say %x; > say qq{@{[%x]}}; > ' 0 a1 a 1

        -- Ken

Re: Parse handler for Attributes in XML
by tangent (Deacon) on Apr 10, 2012 at 10:13 UTC
    All the attributes are in the hash %attrs:
    for my $attr (keys %attrs) { if ($attr eq 'command') { print "Command: $attrs{$attr}\n"; } }
    To access a specific attribute (e.g. command) just say:
    my $command = $attrs{command};
      Thanks a lot Tangent, your piece of advice worked well. I still have some specifics to handle will come back incase i am not able to get it myself:-). Thanks for your time.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2014-10-20 11:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (75 votes), past polls