[Chicago-talk] XML::parser

/dev/mike0 mikeferrari8 at yahoo.com
Tue Feb 20 19:57:54 PST 2007


One Mike F to another Mike F ... thanks!

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

----- Original Message ----
From: Mike Fragassi <frag at ripco.com>
To: Chicago.pm chatter <chicago-talk at pm.org>
Sent: Tuesday, February 20, 2007 3:29:03 PM
Subject: Re: [Chicago-talk] XML::parser


Basically, you need to use the start and end handlers to keep track of 
when you enter and leave your chosen tag, in a separate variable.
Then the character handler checks this variable, and prints if it's ok.
(See my edits below.)

Note that this is very simple, and falls down in more complicated xml. 
E.g. if you have complicated nesting of tags:
   <url> foo <bar>x</bar> </url>
then this approach will print the 'x' as well as the 'foo'.  If that's 
a concern, you can use a stack instead of a single variable to hold the
xml tag names.

Also, it's probably a good idea to use "$element eq 'url'" instead of a 
regex match.

-- Mike F.

On Tue, 20 Feb 2007, /dev/mike0 wrote:
>
>

> 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);

my $inside_url_tag;

> sub startElement {
>      my( $parseinst, $element, %attrs ) = @_;
>        if ($element =~ /url/) {
>        print "$element";
          $inside_url_tag = 1;
>       }
> }
>
> sub endElement {
>      my( $parseinst, $element ) = @_;
>      # do nothing, but stay quiet
        if ($element =~ /url/) {
           $inside_url_tag = 0;
        }
> }
>
> sub characterData {
>      my( $parseinst, $data ) = @_;
#        if ($data = /url/) {
          if ($inside_url_tag) {
>             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/
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk
>
>
>
>
>
>
>
> ____________________________________________________________________________________
> Need Mail bonding?
> Go to the Yahoo! Mail Q&A for great tips from Yahoo! Answers users.
> http://answers.yahoo.com/dir/?link=list&sid=396546091
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk
>
_______________________________________________
Chicago-talk mailing list
Chicago-talk at pm.org
http://mail.pm.org/mailman/listinfo/chicago-talk





 
____________________________________________________________________________________
Have a burning question?  
Go to www.Answers.yahoo.com and get answers from real people who know.


More information about the Chicago-talk mailing list