Hi, I have a file with records written in XML format and I am trying to use XML::Simple to parse the data.
However when I run the code I get the following error "Attempt to bless into a reference at /usr/local/share/perl/5.10.1/XML/Simple.pm line 128, <$xmlfh> line 22"
I dont understand the reference to the variable $xmlfh?
The code is as follows:
use strict; use XML::Simple; use Data::Dumper; #-------------# # Variables #-------------# my $logfile="c:\\FTP_Scripts\\logfile.txt"; my $numArgs= $#ARGV + 1; my $argnum; my $error_time_stamp; my $err; my $debug=0; my $xmlfile; #name of xml file including file path my $xmlfh; #xml file handle my $csvfile; #name of csv output file my $csvfh; #csv file handle my $csvdir; #csv file directory my $current_line; my @data_set; # array to hold all the records in the xml file my $dataset_id = 'REC id'; #sub-string used to identify the lines wit +h data my $current_timestamp; my $previous_timestamp; #-------------# # Main #-------------# ## Get command line parameters if($numArgs < 1){ print_usage(); exit; } if($ARGV[0]){ $xmlfile=$ARGV[0]; $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp > Input XML file: $xmlfile"); } if($ARGV[1]){ $csvdir=$ARGV[1]; $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp > Output directory: $csvdir"); } ## Open data file for reading if( open($xmlfh, '<', "$xmlfile") ){ $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp >Successfully opened file: $xmlfile"); } else{ $error_time_stamp = geterror_time_stamp(); logit("$error_time_stamp > Failed to open file: $xmlfile"); } my $result; my $line_num = 0; ## Get the lines with data, string starts with "^REC id.*" while( $current_line = <$xmlfh> ){ chomp ($current_line); # Identify the lines that begin with "REC id" $result = index($current_line,$dataset_id); if( $result == 1 ){ $data_set[$line_num] = $current_line; $line_num = $line_num + 1; } } my $xml_ref; my $xs; my $x; for($x==0; $x < $line_num; $x++){ print "\n\n$x : $data_set[$x]\n"; $xs = new XML::Simple->new(); $xml_ref = $xs->XMLin($data_set[$x]); print Data::Dumper->Dump($xml_ref); } close $xmlfh; exit 0; #--------------# # Sub-routines #--------------# sub logit { my $line=shift; open(LOG,">>$logfile") or die("$! Can't open $logfile"); if($debug){ print "$line\n"; } print LOG "$line\n"; close LOG; } sub geterror_time_stamp { my ($sec, $min, $hour, $day, $month, $yr19, @rest) = localtime(time) +; $error_time_stamp = $month . "-" . $day . "-" . ($yr19 + 1900) . " " + . $hour . ":" . $min . ":" . $sec; return $error_time_stamp; } sub print_usage{ print "\nNo files were specified,\nCorrect usage is:\n"; print "xml-csv.pl <XML input file> [output directory, optional]\n\ +n"; return; }
In reply to XML::Simple "Bless" error by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |