Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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

Replies are listed 'Best First'.
Re: Parse handler for Attributes in XML
by tangent (Priest) 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.
Re: Parse handler for Attributes in XML
by kcott (Canon) 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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://964291]
Front-paged by Corion
[1nickt]: Ah, you got Linus' fave? With Ubuntu? 4k display?
[1nickt]: ( The 17" monster I just bought is for my wife, who doesn;t leave the house with it...)
[Corion]: 1nickt: No, I'm using it with Windows 10, but with the 4k display, yes. It's a very nice build IMO, and relatively sturdy. Also, it got 8 or 9 on the ifixit repair scale, which I consider good
[Corion]: ... especially as I expect to have to replace the battery set in two or three years' time
[1nickt]: Nice. So happy that Dells are still user-repairable. Unusual these days.

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2017-03-27 13:10 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (320 votes). Check out past polls.