XML Parsing in Perl

Discussion in 'Perl Misc' started by Deepan Perl XML Parser, Mar 19, 2008.

  1. Hi all,
    This post is regarding XML parsing in perl.

    I have written a handler for capturing Char named
    characterData. when this handler is getting called up for parsing
    ""1667999478"" which is available in $data it should be able
    to print "1667999478" replacing " with ", but it is only printing
    ".

    Here is the code :

    sub characterData {
    my( $parseinst, $data ) = @_;
    print "$data";
    ------------------>>> here i should get "1667999478" instead of "
    }

    Please help me.

    Thanks,
    Deepan
     
    Deepan Perl XML Parser, Mar 19, 2008
    #1
    1. Advertising

  2. Deepan Perl XML Parser

    Ben Bullock Guest

    On Wed, 19 Mar 2008 05:04:12 -0700, Deepan Perl XML Parser wrote:

    > This post is regarding XML parsing in perl.
    >
    > I have written a handler for capturing Char named
    > characterData. when this handler is getting called up for parsing
    > ""1667999478"" which is available in $data it should be able
    > to print "1667999478" replacing " with ", but it is only printing
    > ".
    >
    > Here is the code :
    >
    > sub characterData {
    > my( $parseinst, $data ) = @_;
    > print "$data";
    > ------------------>>> here i should get "1667999478" instead of "
    > }


    #!/usr/bin/perl
    use warnings;
    use strict;
    use XML::parser;
    my $parser = XML::parser->new(Handlers => {Char =>\&handle_char});
    my $text = "<tag>";
    for my $i (1..10000) {$text .=
    "XML::parser Gotcha: Character Handler Is Often Called Multiple Times\n"}
    $text .= "</tag>";
    $parser->parse($text);
    my $x;
    sub handle_char
    {
    my ($ignore,$text) = @_;
    $x++;
    print "Text is \"$text\". I have now been called $x times.\n";
    }
     
    Ben Bullock, Mar 19, 2008
    #2
    1. Advertising

  3. Deepan Perl XML Parser <> wrote:

    > This post is regarding XML parsing in perl.



    It would have been helpful if you had shared what XML parsing
    module you are using.

    It would have been even more helpful if you had posted a short
    and complete program *that we can run* that illustrates your problem.

    Have you seen the Posting Guidelines that are posted here frequently?


    > I have written a handler for capturing Char named
    > characterData. when this handler is getting called up for parsing
    > "&quot;1667999478&quot;"
    > which is available in $data it should be able



    Does "available in $data" mean that that is the output of the
    print statment in your code?


    > to print "1667999478" replacing &quot; with ", but it is only printing
    > ".



    That would be expected behavior with some XML parsing modules.

    What XML parsing module you are using?

    Many (all?) modules can call characterData() multiple times.

    If there are 20 character data, then it may be called as many
    as 20 times.

    The usual approach is to collect the character data into a buffer,
    then operate on the buffer from the appropriate end tag handler.


    > Here is the code :
    >
    > sub characterData {
    > my( $parseinst, $data ) = @_;
    > print "$data";



    perldoc -q vars

    What's wrong with always quoting "$vars"?


    > ------------------>>> here i should get "1667999478" instead of "
    > }
    >
    > Please help me.



    Pleading is counter-productive, it makes it *less* likely that
    someone will actually help you.

    Have you seen the Posting Guidelines that are posted here frequently?


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Mar 20, 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. GIMME
    Replies:
    2
    Views:
    877
    GIMME
    Feb 11, 2004
  2. John Bokma

    Perl: parsing XML, what is needed?

    John Bokma, Aug 7, 2003, in forum: XML
    Replies:
    1
    Views:
    441
    Bjoern Hoehrmann
    Aug 7, 2003
  3. zzapper
    Replies:
    2
    Views:
    813
    zzapper
    Sep 30, 2004
  4. John Levine
    Replies:
    0
    Views:
    738
    John Levine
    Feb 2, 2012
  5. Erik Wasser
    Replies:
    5
    Views:
    468
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page