Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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 (Chancellor) 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
[hippo]: Yary: There are a few useful pointers in I need perl coding standards
[hippo]: ... and a boatload of good stuff in On Coding Standards and Code Reviews
[Yary]: Looking at system pm's, looks like it's "package" on the first line, then POD, then "use;use strict; use warnings; use modules..."
[Yary]: ah thanks for those links
[hippo]: Just bear in mind that all such ideas are subjective.
[choroba]: There might be a different standard at the shop you write the code for.
[choroba]: The order of package - (strict+warnings | warnings+strict) - modules makes sense, POD and strict-warnings order are not so important

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2018-01-23 17:02 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (250 votes). Check out past polls.