Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: XML::Simple throws decode error in

by almut (Canon)
on Aug 03, 2010 at 23:15 UTC ( [id://852768] : note . print w/replies, xml ) Need Help??

in reply to XML::Simple throws decode error in

I think it would help if you could provide a complete, runnable example that allows to reproduce the error.

I played around a bit, but the only thing remotely similar I managed to produce is

Cannot decode string with wide characters at /usr/lib/perl/5.8/Encode. +pm line 166.

when I do exactly what it complains about, i.e. pass XMLin() a string with already decoded (wide) characters...

What is line 42 in, is this the line with $xml->XMLin(...)?  Your subject says it "throws decode error in", but you show a different error (or rather warning) message...(?)

Replies are listed 'Best First'.
Re^2: XML::Simple throws decode error in
by Anonymous Monk on Aug 04, 2010 at 18:12 UTC
    Shoot! I pasted the wrong error. I meant to say that the error printed is

    Cannot decode string with wide characters at C:/Perl/lib/ line 174.

    Okay, I found a way to reduplicate it. Place the following lines in a text file and save it as utf8:

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>

    <etax id="{e961ee2c-a029-489a-8bf4-3c2ecef7f019}" ettx="TL-Scriptures.ettx">


    <LEX st="Kasulatan" id="tl" chrBrk="" tSt="s11"/>

    <LEX st="Pambungad" id="tl" chrBrk="" tSt="p11"/>

    <LEX st="Panimula" id="tl" chrBrk="" tSt="h11"/>


    The following is the module I wrote where the error occurs:

    #!/usr/bin/perl -l package SIFX; use strict; use XML::Simple; use Data::Dumper; sub new(){#scalar file name optional my $class = shift; my $self = { etaxFile => '', SIFX => {}, }; bless $self, $class; load($self,shift) if @_ ==1; return $self; } sub load(){ return -1 if(@_ == 0); my ($self, $input) = @_; my $sifx; if((substr $input, -4, 4) eq '.txt'){#if input was a file name $self->{etaxFile} = $input; open my $etax, '<utf8', $input or print "Could not open etax f +ile at __LINE__"; my $text; while($text ne '<sifx>'){#until beginning of SIFX chomp($text = <$etax>); } $sifx= '<sifx>'; do{#until end of SIFX chomp($text = <$etax>); $sifx .= $text; }while($text !~ m#</sifx>#); close $etax; } else{ $sifx = $input; } my $xml = XML::Simple->new(); $self->{SIFX} = $xml->XMLin($sifx); print "SIFX hash is : "; print Dumper($self->{SIFX}); } 1;

    And you can test it with the following after changing the $testSifx variable to the path of the text file:

    my $xml = XML::Simple->new(); my $testSifx = "C:\\Users\\nate\\Desktop\\testSIFX.txt"; my $sifx = SIFX->new($testSifx); print Dumper($sifx);

    I apologize for the original, inadequate, inaccurate post.

      As already hinted at, XMLin() doesn't like already decoded input; it wants bytes/octets.  IOW, simply open the file as

      open my $etax, '<', $input or ... # ^ no :utf8

        Works like a charm! Thanks!

        Works great now! Thanks!