kanz0r has asked for the wisdom of the Perl Monks concerning the following question:

hello fellow monks, I am working with XML parser using XML::Twig module and I ran into trouble extracting href or id value.
For example,
-<NBA> <HomeTeam href="Celtics"/> <VisitorTeam href="Bulls"/> ........................ <Stadium id="FleetCenter"> </NBA>
what is right way to extract value within "" and assign $HomeTeam = Celtics, etc...
any assistance would be appreciated, thanks!
-kanz0r

Replies are listed 'Best First'.
Re: extracting href data in XML
by borisz (Canon) on Aug 07, 2005 at 09:30 UTC
    Here is a example. Remember that perl is casesensitive. use strict; use warnings;
    use strict; use warnings; use XML::Twig; my $tw = XML::Twig->new( TwigHandlers => { 'NBA/HomeTeam' => \&nba } ) +; $tw->parse(<<__XML__); <NBA> <HomeTeam href="Celtics"/> <VisitorTeam href="Bulls"/> <Stadium id="FleetCenter">blabla</Stadium> </NBA> __XML__ $tw->flush; sub nba { my ( $twig, $nba ) = @_; $nba->set_att( href => 'MyHomeTeam' ); $twig->flush; }
    Boris
      thank you very much!
      still can't figure one thing out, why i can't use the following code just to get the data "Celtics"
      $hometeam -> ('att') -> ('href');

        Because att is a method, that you call with the syntax $hometeam ->att('href');?

Re: extracting href data in XML
by marto (Cardinal) on Aug 07, 2005 at 08:52 UTC
    There are examples of this on the XML::Twig site.

    Martin
      sorry but I am new to perl and don't know where exactly is the information regarding to extract href part, can you help me by telling which example is the exact one i should be looking for?
Re: extracting href data in XML
by Anonymous Monk on Aug 07, 2005 at 08:48 UTC
    What have you tried? There are examples in the documentation.
      i have tried the following code:
      use strict; use XML::Twig; my $twig = new XML::Twig(TwigHandlers=> {NBA=>\&NBA}); $twig ->parsefile('nba.xml'); exit<p> sub nba { my ($twig, $nba)=@_; $nba{stadium}= join ':', map {$_->text || ''}@{[$nba->children( 'S +tadium')]; print "$nba(stadium)\n"; $nba ->delete; }

      CODE tags added by Arunbear