XML Parsing in Perl

  • Thread starter Deepan Perl XML Parser
  • Start date
D

Deepan Perl XML Parser

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
 
B

Ben Bullock

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";
}
 
T

Tad J McClellan

Deepan Perl XML Parser said:
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?
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top