How to store correctly UTF-8 texts (after XML parsing)

F

Francesco Moi

Hello.

I'm parsing an XML file, and storing some of the contents into
a MySQL database. I use Perl (XML::parser).

I was adviced that 'XML::parser' works with UTF-8 charset, so
I use 'Unicode::MapUTF8' to convert text.

But:
* If I parse the XML file, and print the text, I get 'música' (OK).
* If I parse the XML file, store the text, and print the
stored text, I get 'música' (wrong)
* If I parse the XML file, store the text, convert the stored text
from UTF-8 to ISO-8859-1 charset, and print it, I get 'música' (OK).
* If I parse the XML file, convert the text from UTF-8 to ISO-8859-1
charset, store it, and print the stored (and converted) text,
I get 'música' (wrong, but I did not expect it).

I only want to store text into my database and print it correctly,
but converting it _BEFORE_ storing. I don't feel like converting
each text I read from the database.

The piece of code I use to convert text is:
--------//----------
use Unicode::MapUTF8 qw(to_utf8 from_utf8 utf8_supported_charset);
sub convert_unicode
{
$my_text = $_[0];
my $converted = from_utf8({ -string => $my_text, -charset => 'ISO-8859-1' });
return $converted;
}
--------//---------

Does anybody know what I am doing wrong? Thank you very much.
 

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

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top