Fellow Monks,

After a lot of documentation reading and testing and spending almost 2 days trying to understand XML:Parser module I finally have to ask for some help here.

I receive some data in a XML file and am required to extract part of the data

<rootNode> <a> ...text + other XML </a> <a> ...text + other XML </a> </rootNode>

Now I need to extract all information between

<a> and </a>
tags and I need to do this using XML:Parser.
Any suggestions on how I could do this ?
below is my non-working code:
use XML::Parser; my $XmlFile = "foo.xml"; die "Can't find file \"$XmlFile\"" unless -f $XmlFile; my $pFoundFlag = 0; my $NewFile; my $parser = new XML::Parser(Style => 'Debug'); $parser->setHandlers( Start => \&startElement(), Char => \&char_handler(), End => \&end_Handler, ); $parser->parsefile($XmlFile); sub startElement { my ($parserInst, $element, %attr) = @_; if ($element eq "a") { $pFoundFlag = 1; } if ($pFoundFlag) { if(not $NewFile) { $NewFile = $element; } else { $NewFile .= $element; } } } sub char_handler { my ($parserInst, $data); if ($pFoundFlag) { $NewFile .= $data; } } sub end_Handler { my ($parserInst, $element); if ($pFoundFlag) { $NewFile .= $element; } if ($element eq "a") { $pFoundFlag = 0; } } $OpFile = "foo_outpur.xml"; open (OP, ">$OpFile") or die ("can't open output file!"); print OP $NewFile; close(OP);

In reply to XML processing by shreya

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