enabling utf8 encoding for sax parer XML::SAX::Expat
eyal edri <[email protected]>
| Newsgroups | gmane.comp.lang.perl.xml |
|---|---|
| Message-ID | <[email protected]> |
Hi guys,
i'm using the sax2 driver/parser XML::SAX::Expat to parse xml files, using
the following code:
sub parseXmlFile
> {
> my ($self, $srcFile, $targetFile, $deleteSrc, $saxParserName) = @_;
>
> $XML::SAX::ParserPackage = 'XML::SAX::Expat';
> #dynamicly build use code for using custom xml sax parser
> my $use = "use $saxParserName;";
> my $make = "return (XML::SAX::ParserFactory->parser( Handler =>
> $saxParserName->new( \"$targetFile\" )));";
>
> my $parser = eval ($use.$make);
>
> $parser->parse_uri($srcFile);
> unlink $srcFile if ($deleteSrc);
>
> return 1;
> }
>
where $saxParserName is the name of the PM i wrote containing the callbacks
for sax (listed in the end of this email).
i'm getting an error while parsing an xml with non-ascii chars: "not well
formed xml at line..... "
a few keys points:
1. i don't have control of the src xml, so i can't make them give it to
me properly.
2. i tried to adding the magic bits 'EF BB BF' at the begining of the xml
before parsing it, and it worked. (the file command also told it's UTF8).
so my question is,
- how can i tell the parser to open the xml in a certain encoding
(despite the sender fucked up and didn't add the magic numbers) ?
- should i use a different sax parser?
and should i always use :encoding(UTF-8) when opening/writing files?
thanks!!
----------------------------------------------------------------------------------------------------------------------
> package FeedsSaxParser;
>
> use utf8;
> use base qw(XML::SAX::Base);
> @ISA = ('XML::SAX::Base');
>
> sub new
> {
> my ($class, $filename) = @_;
> my $self = $class->SUPER::new();
> eval
> {
> open my $fh ,">", $filename;
> $self->{fh} = $fh;
> };
> if ($@)
> {
> print STDERR "error creating FeedsSaxParser: $@";
> return;
> }
>
> $self;
> }
>
> sub start_element
> {
> #some code.......
> }
>
> sub end_element
> {
> #.....code
> }
>
> sub characters
> {
> my $self = shift;
>
> #.......code
>
> }
>
> }
>
--
Eyal Edri
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs