Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > XML::Parser Q: The Char handler always returns '1'

Reply
Thread Tools

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

 
 
Swapnajit Mitra
Guest
Posts: n/a
 
      02-13-2008
Here is the code fragments:

================================================== ============
# Parser command
my $parser = new XML:arser (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.
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      02-14-2008

Quoth Jim Gibson <>:
>
> 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

 
Reply With Quote
 
 
 
 
Swapnajit Mitra
Guest
Posts: n/a
 
      02-14-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char *fred; char * fred; char *fred; any difference? Ben Pfaff C Programming 5 01-17-2004 07:37 PM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57