[Chicago-talk] PM T-Shirts

/dev/mike0 mikeferrari8 at yahoo.com
Tue Feb 20 10:46:40 PST 2007


Hi Everyone

I am writing a small script for work to parse an xml doc and print out data from a specific tag. Its on a windows xp box with active state perl installed, no additional modules, just the defaults. I usually use XML:Simple but am wanting to learn a bit more and understand XML:Parser. I create handlers according to the perldoc and have been using an online example (http://builder.com.com/5100-6371_14-1044612-2.html) Listing A and B for reference..

I can print out start tags, character data, and end tags for the entire xml file.. My big question is.. how the heck do i just catch one specific tag? I tried using a if statement and match operator to seach for specifics but none seem to be working.

maybe its just me, its sounds pretty noob, i am feeling a bit off today.. :-) i would think its pretty simple and am sort of stuck :-)

So using the data.xml (listing B on the web page) ..the following is the approach i am taking.


use XML::Parser;
my $xmlfile = shift;
die "Cannot find file \"$xmlfile\""
       unless -f $xmlfile;
my $parser = new XML::Parser;
$parser->setHandlers( Start => \&startElement,
                        End => \&endElement,
                       Char => \&characterData,
                    Default => \&default);
$parser->parsefile($xmlfile);

sub startElement {
      my( $parseinst, $element, %attrs ) = @_;
        if ($element =~ /url/) {
        print "$element";
       }
 }

sub endElement {
      my( $parseinst, $element ) = @_;
      # do nothing, but stay quiet
 }

sub characterData {
      my( $parseinst, $data ) = @_;
        if ($data = /url/) {
        print "$data\n ";
        }
 }

sub default {

      my( $parseinst, $data ) = @_;
        # do nothing, but stay quiet
 }


The problem as i think it boils down to is.. if the xml tag is <url> i want to print out the character data between the start and end tag for it.. basically <url> print whats here</url>.

Seems simple. but i am stuck.

Thanks
Mike
 
http://www.mikeferrari.com
http://www.chromeczars.com
http://www.hunnertcarpileup.com





 
____________________________________________________________________________________
It's here! Your new message!  
Get new email alerts with the free Yahoo! Toolbar.
http://tools.search.yahoo.com/toolbar/features/mail/


More information about the Chicago-talk mailing list