Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

UTF-8 problem parsing XML

by rinceWind (Monsignor)
on Apr 14, 2007 at 14:17 UTC ( #610066=perlquestion: print w/replies, xml ) Need Help??
rinceWind has asked for the wisdom of the Perl Monks concerning the following question:

I have the following fairly simple script to parse BookMooch data:

#!/usr/bin/perl use strict; use warnings; use CGI qw(:standard); use CGI::Carp; use WWW::Mechanize; use XML::Simple; use YAML; use Encode; my $mech = WWW::Mechanize->new; my $api_base = ''; my $q = CGI->new; my $user = $q->param('user'); $mech->get("$api_base?userids=$user"); die "Failed to get user $user from BookMooch" unless $mech->success; print header, start_html, "\n" ; my $xml = $mech->content; # $xml = encode('iso-8859-1', $xml); (doesn't fix the problem) my $data = XMLin($xml); print pre(Dump($data)), end_html;

When I run it, I get the following output:

Content-Type: text/html; charset=ISO-8859-1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns="" lang="en-US" xml:lang="en-U +S"> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> </head> <body> :1492: parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0xA3 0x31 0x20 0x31 <condition>Good condition. 1970 edition with n +et cover price shown as 1 15s + ^ at /usr/lib/perl5/XML/LibXML/SAX/ line 31

Uncommenting the line with encode, doesn't make any difference. I'm new to UTF-8 and encoding. What's the correct incantation for what I'm doing?

The input data is Latin-1 as far as I'm aware, and it's b0rking on a pound sign '£'

Any help would be much appreciated

Apprentice wetware hacker

Replies are listed 'Best First'.
Re: UTF-8 problem parsing XML
by Joost (Canon) on Apr 14, 2007 at 15:54 UTC
    Assuming the input file is really latin-1, you can do a couple of things:

    • Add an XML declaration with an encoding attribute to the XML file - <?xml encoding="iso-8859-1"?> - that should make sure the XML parser will do the right thing.
    • Convert the XML to utf8, which you've tried but you got it backwards. The correct statement is $xml = decode('iso-8859-1', $xml);
    • Figure out some other way to signal the real encoding to XML::Simple. Not sure if you can.

      Thanks! Changing encode to decode did the trick.

      Apprentice wetware hacker

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://610066]
Approved by wfsp
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2018-02-20 14:44 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (271 votes). Check out past polls.