shreya has asked for the wisdom of the Perl Monks concerning the following question:
Fellow Monks,
Any suggestions on how I could do this ?
below is my non-working code:
===============================================
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
tags and I need to do this using XML:Parser.<a> and </a>
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);
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: XML processing
by borisz (Canon) on Aug 05, 2005 at 22:55 UTC | |
Re: XML processing
by Tanktalus (Canon) on Aug 06, 2005 at 02:44 UTC | |
by shreya (Novice) on Aug 10, 2005 at 22:31 UTC | |
by mirod (Canon) on Aug 10, 2005 at 23:13 UTC | |
by shreya (Novice) on Aug 11, 2005 at 19:07 UTC | |
by runrig (Abbot) on Aug 11, 2005 at 19:11 UTC | |
by graff (Chancellor) on Aug 06, 2005 at 16:24 UTC | |
Re: XML processing
by mrborisguy (Hermit) on Aug 06, 2005 at 03:26 UTC | |
Re: XML processing
by shreya (Novice) on Aug 08, 2005 at 13:50 UTC | |
Re: XML processing
by shreya (Novice) on Aug 10, 2005 at 22:59 UTC |
Back to
Seekers of Perl Wisdom