I'm writing an object oriented parser for complex XML documents (blast output). I need to parse certain fields out of a complex tree and I want to skip over the ones that I don't need (or avoid saving the data).

Hit_num is the first element that I'd like to save in the file (it is a child element of other things that I don't want to save).

The main problem (right now) is that I'm not sure if this is going to work with object oriented style and whether I should just dump that part. What I'm running into is that I can't pass variables between the start_handler, char_handler, and end_handler. I don't know if there's a way in XML::Parser to do this, I've looked at the docs and can't figure that part out. Perhaps this is because I'm using parsefile instead of parse?

As you can probably tell by now, this is my first script involving XML and any advice would be appreciated.

-----
my $pack = NCBIXML->new; $pack->createHandlers; my $parser = $pack->retrieve('HNDL'); $parser->parsefile($file); my %data = $parser->retrieve('RES'); ############## sub createHandlers { my $obj = shift; my $parser = new XML::Parser(ErrorContext=>2); $parser->setHandlers( Start => \&start_handler); $parser->setHandlers( End => \&end_handler); $parser->setHandlers( Char => \&char_handler); $obj->{'HNDL'} = $parser; } sub start_handler { my ($obj, $element, %attrs) = @_; $tag = $element; # primary key for the array $obj->{'TAG'} = $tag; } sub char_handler { my ($obj, $data) = @_; my $tag = $obj->retrieve('TAG'); my %info = $obj->retrieve('RES'); if ($tag eq 'Hit_num') { $key = $data; next; } $info{$key}{$tag} = $data; $obj->{'RES'} = \%info; }
Thanks for any help. Ineff

In reply to XML::Parser and objects by Ineffectual

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.