Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

In reply to Parse handler for Attributes in XML by balajinagaraju

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-19 08:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found