(Actually, this isn't a question, but a solution in case others are looking for the same nugget of wisdom.)

I have incoming data in XML that's UTF-8 encoded. It includes special symbols such as nonbreaking spaces, the registered trademark symbol (R), the "TM" symbol, Greek letters, etc. This data is being put into an Oracle database via script that reads the XML, parses it using XML::Twig, and saves it to the database via DBI. In order to preserve these characters, here's what I had to do:

1) Add this line to Perl scripts:

use encoding "utf-8"; # For text containing I +SO-8859-1 and/or UTF-8 characters

2) Add the encoding qualifier to any 'open' statements on files that contain UTF-8:

open (OUT, ">:encoding(UTF-8)", ">$outfile") || die "Unable to open $o +utfile for writing.";

3) Add this line if interfacing with an Oracle database:

$ENV{NLS_LANG} = 'AMERICAN_AMERICA.AL32UTF8'; # Handle UTF-8 charac +ters

4) Modify the Oracle login to include the 'ora_charset => "UTF8"' attribute in the connect string:

my $dbh = DBI->connect("dbi:Oracle:$DATABASE", $USERNAME, $PASSWORD, { RaiseError => 1, AutoCommit => 0, ora_charset => "UTF8" }) or die ($DBI::errstr . "\n");

5) If processing XML files with encoding="UTF-8" via XML::Twig, add the output_encoding attribute to the call:

$twig = XML::Twig->new( twig_handlers => $handlers, output_encoding => + 'utf-8' );


In reply to Solved: Preserving UTF-8 characters in Oracle and XML by poltr1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.