XML::Parser Q: The Char handler always returns '1'

S

Swapnajit Mitra

Here is the code fragments:

==============================================================
# Parser command
my $parser = new XML::parser (ErrorContext => 2);
$parser->setHandlers(Start => \&start_handler
, Char => \&char_handler
, End => \&end_handler
, Default => \&default_handler
);
$retVal = $parser->parsefile($file);
....
sub char_handler {
my ($p, $data) = @_;


print "char_handler: data = *$data*\n";
==============================================================


The last print statement always prints '1' with the following input
(even for NAME or VALUE fields). Subroutines for Start or End seem to
work just fine.


==============================================================
<P1>
<T1> T1_TXT </T1>
<F1>
<F2>
<NAME> MY_NAME </NAME>
<VALUE> 1 </VALUE>
</F2>
....
==============================================================


Any help will be greatly appreciated.
 
B

Ben Morrow

Quoth Jim Gibson said:
Here is a question for Perl gurus: how does one use the special file
handle DATA to feed data to parsefile(). parsefile(DATA) didn't work
for me:

Couldn't open DATA:
No such file or directory at swapnajit2.pl line 14

->parsefile accepts a filename, so passing it an (unquoted) string is
going to try to open that as a file. DATA is a filehandle, so you need
to pass it to ->parse; however, a simple unquoted DATA is a string,
which ->parse will try to read XML from directly. You have to pass a
proper reference:

$XML->parse(\*DATA, ...);

You should always pass bareword filehandles like this: as you can see,
it's safer. Don't try to get away with just *DATA: bare globs are very
magical, and can behave rather oddly.

Ben
 
S

Swapnajit Mitra

Jim,

I finally made it work. It was a mistake that I had made in parsing
the $data variable (as you correctly suspected).

Thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top