Keeping in mind that \311 is the iso-8859-1 encoding of U+00C9, and that \303\211 is the UTF-8 encoding of the same character, you can see that XML::Simple properly extracts text from XML:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper qw( Dumper ); use XML::Simple qw( ); $XML::Simple::PREFERRED_PARSER = 'XML::Parser'; my $latin1_xml = <<"__EOI__"; <?xml version="1.0" encoding="iso-8859-1"?> <root>\311ric</root> __EOI__ my $utf8_xml = <<"__EOI__"; <?xml version="1.0" encoding="UTF-8"?> <root>\303\211ric</root> __EOI__ my $xs = XML::Simple->new(); for my $xml ($latin1_xml, $utf8_xml) { my $tree = $xs->XMLin($xml, ForceArray => 1, KeepRoot => 1, ); local $Data::Dumper::Useqq = 1; print Dumper $tree; }
$VAR1 = { 'root' => [ "\x{c9}ric" ] }; $VAR1 = { 'root' => [ "\x{c9}ric" ] };

It also outputs XML properly (albeit using a weird interface):

#!/usr/bin/perl use strict; use warnings; use Data::Dumper qw( Dumper ); use XML::Simple qw( ); my $tree = { 'root' => [ "\x{c9}ric" ] }; $XML::Simple::PREFERRED_PARSER = 'XML::Parser'; my $xs = XML::Simple->new(); for my $enc (qw( iso-8859-1 UTF-8 )) { my $xml = ''; { open(my $fh, ">:encoding($enc)", \$xml) or die; $xs->XMLout($tree, XMLDecl => qq{<?xml version="1.0" encoding="$enc"?>}, KeepRoot => 1, OutputFile => $fh, ); close($fh); } local $Data::Dumper::Useqq = 1; print Dumper $xml; }
$VAR1 = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n<root>\311ri +c</root>\n"; $VAR1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\303\211ric +</root>\n";

In reply to Re: UTF8 and XML by ikegami
in thread UTF8 and XML by clintonm9

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.