So I wrote a parser routine to take one xml file and reparse into another one. This code I later modified to split a large xml file into many small xml files.
I am having a problem with an output. Parsing works fine the only thing output also includes unwanted strings like HASH(0x19f9b58), I am not sure why and need set of friendly eyes.
use Encode;
use XML::Parser;
my $parser = XML::Parser->new( Handlers => {Start => \&handle_elem_start,
End => \&handle_elem_end,Char => \&handle_char_data,});
my $record;
my $file = shift @ARGV;
if( $file ) {$parser->parsefile( $file );}
exit;
sub handle_elem_start
{
my( $expat, $name, %atts ) = @_;
if ($name eq 'articles'){$file="_data.xml";unlink($file);}
$record .= "<";
$record .= "$name";
foreach my $key (keys %atts){$record .= " $key=\"$atts{$key}\"";}
$record .= ">";
}
sub handle_char_data
{
my( $expat, $text ) = @_;
$text = decode_utf8( $text );
$record .= "$text";
}
sub handle_elem_end
{
my( $expat, $name ) = @_;
$record .= "</$name>";
if( $name eq 'article' )
{
open (MYFILE, '>>'.$file);
print MYFILE $record;
close (MYFILE);
print $record;
$record = {};
}
return unless( $name eq 'article' );
}
Sample output:
...
</article>HASH(0x19f9b40)
<article doi="10.1103/PhysRevSeriesI.9.304">
<journal short="Phys. Rev. (Series I)" jcode="PRI">Physical Review (Series I)</journal>
<volume>9</volume>
<issue printdate="1899-11-00">5</issue>
<fpage>304</fpage>
<lpage>309</lpage>
<seqno>1</seqno>
<price></price><tocsec>Articles</tocsec>
<arttype type="article"></arttype><doi>10.1103/PhysRevSeriesI.9.304</doi>
<title>An Investigation of the Magnetic Qualities of Building Brick</title>
<authgrp>
<author><givenname>O.</givenname><middlename>A.</middlename><surname>Gage</surname></author>
<author><givenname>H.</givenname><middlename>E.</middlename><surname>Lawrence</surname></author>
</authgrp>
<cpyrt>
<cpyrtdate date="1899"></cpyrtdate><cpyrtholder>The American Physical Society</cpyrtholder>
</cpyrt>
</article>HASH(0x19f9b58)
...
HASH strings are not wanted, please advise.