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

Discussion in 'Perl Misc' started by Swapnajit Mitra, Feb 13, 2008.

  1. 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.
     
    Swapnajit Mitra, Feb 13, 2008
    #1
    1. Advertising

  2. Swapnajit Mitra

    Ben Morrow Guest

    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
     
    Ben Morrow, Feb 14, 2008
    #2
    1. Advertising

  3. 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.
     
    Swapnajit Mitra, Feb 14, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lovecreatesbeauty
    Replies:
    1
    Views:
    1,058
    Ian Collins
    May 9, 2006
  2. arne
    Replies:
    0
    Views:
    354
  3. Erik Wasser
    Replies:
    5
    Views:
    463
    Peter J. Holzer
    Mar 5, 2006
  4. Sean
    Replies:
    3
    Views:
    278
    robic0
    Oct 3, 2006
  5. Sean
    Replies:
    0
    Views:
    369
Loading...

Share This Page