On 19/03/2012 13:10, Anirban Adhikary wrote:
Hi List,
I have a XML file which looks like as follows

<ISProducts>
<StoreInfo>
    <BSC id="AMIBRB1">
       <ALPHA>10</ALPHA>
       <AMRCSFR3MODE>1,3,4,7</AMRCSFR3MODE>
       <AMRCSFR3THR>12,16,21</AMRCSFR3THR>
       <AMRCSFR3HYST>2,3,3</AMRCSFR3HYST>
       <AMRCSFR4MODE>1,3,6,8</AMRCSFR4MODE>
       <AMRCSFR4THR>12,17,25</AMRCSFR4THR>
       <PAGBUNDLE>50</PAGBUNDLE>
       <USERDATA>AMI_BRANLY_B_1</USERDATA>
     </BSC>
.........................
.............................

Now my question is how to extract the value of id in a variable using
XML::Twig.
Since the xml file is quite large I like to print the value of id using a
loop.

XML::Twig uses callbacks to process pieces of the XML that you have
defined. In this case you are interested only in the <BSC> start tag so
you can define a "start tag handler". Using $twig->purge empties the
data read so far. If you use this once you have accessed all the
information you need from a given element there is no need to store the
entire XML data in memory.

The program below doeas what I think you want.

HTH,

Rob


use strict;
use warnings;

use XML::Twig;

my $twig = XML::Twig->new(start_tag_handlers => {
    BSC => \&on_BSC
});

sub on_BSC {
  my($twig, $bsc)= @_;
  print $bsc->id, "\n";
  $twig->purge;
}

$twig->parsefile('ISProducts.xml');




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to