Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

utf8 && XML::Simple

by zakzebrowski (Curate)
on Feb 01, 2005 at 17:07 UTC ( #426964=perlquestion: print w/ replies, xml ) Need Help??
zakzebrowski has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,
Can anyone explain why this fails on perl 5.8.3? I tried various utf8 tricks, but I can't get it to work...
Thanks.
Zak
Update: johnnywang++ and borisz++. Write file out as utf8, but read in a file using XML::Simple's XMLin interface!
use XML::Simple qw(:strict); use Encode; # use open 'utf8'; # Can't get this to work - should open all files as + utf8... use Data::Dumper; my $val; $val->{utfchar} = "\x{10a0}"; my $xml = XMLout($val,KeyAttr=>{item=>'name}); open (OUT,">out.xml"); print OUT $xml; close OUT; # Yes, I could use a different slurp funciton... my $readin=""; open (IN,"<out.xml"); while (<IN>){ $readin = $readin . $_; } close IN; my $result = XMLin($readin,KeyAttr=>{item=>'name'},ForceArray=>1); if ($result->{utfchar} eq "\x{10a0}"){ print "Wohoo!\n"; } else { print "Doh!\n"; }


----
Zak - the office

Comment on utf8 && XML::Simple
Download Code
Re: utf8 && XML::Simple
by borisz (Canon) on Feb 01, 2005 at 17:14 UTC
    What about use open ':utf8';?
    Wohoo! for me.
    Boris
      Thanks for replying. In my (much more complicated real life) file, I get "Cannot decode string with wide characters at /usr/../Encode.pm line 184."


      ----
      Zak - the office
        most likely your input data is arady in utf8 and you want to convert it a second time to utf8. for example:
        use Encode; my $str = "hi"; # hi is a notmal string. $str .= chr(0x1234); # str is now a utf8 string Encode::decode_utf8($str, 1); # here you get the error.
        Propably you exchange encode and decode. Or the decode function call is not needed in your case.
        Boris
Re: utf8 && XML::Simple
by johnnywang (Priest) on Feb 01, 2005 at 18:11 UTC
    Just want to point out (this is not what you're asking) that XMLin can also take a file name as first argument. So instead of:
    # Yes, I could use a different slurp funciton... my $readin=""; open (IN,"<out.xml"); while (<IN>){ $readin = $readin . $_; } close IN; my $result = XMLin($readin,KeyAttr=>{item=>'name'},ForceArray=>1);
    you can just say:
    my $result = XMLin("out.xml",KeyAttr=>{item=>'name'},ForceArray=>1);
      ++ ++ !! This technique works. It looks like XML::Simple will *automatically* read in a file as utf8. So, you must explicilty write a file as utf8, and just use the XMLin method explicitly to read the file... Thanks! Zak


      ----
      Zak - the office

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://426964]
Approved by bart
Front-paged by kutsu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2014-09-16 16:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (33 votes), past polls