I have some XML code that I get with LWP::UserAgent.
The XML is declared and seems to contain ISO-8859-1 data with the header:
<?xml version="1.0" encoding="ISO-8859-1"?>...
I used the following code to get the data:
my $url = 'https://some.url/file.xml'; my $LWP_Data; use LWP::UserAgent; $LWP_Data->{ua} = LWP::UserAgent->new; $LWP_Data->{ua}->timeout(7); $LWP_Data->{feed} = $LWP_Data->{ua}->get($url); if ($LWP_Data->{feed}->is_success) { my $xml = $LWP_Data->{feed}->content; use XML::Simple qw(:strict); my $ref = XMLin( $xml, ForceArray => 1,KeyAttr => [ ]); ...

The results of $ref shows corrupted characters, e.g. ΓΌ instead of ü which made me think that the original data might not be ISO-8859-1 as declared. After some testing I assume that XML::Simple simply does not care about the declared encoding because the following works.

my $url = 'https://some.url/file.xml'; my $LWP_Data; use LWP::UserAgent; $LWP_Data->{ua} = LWP::UserAgent->new; $LWP_Data->{ua}->timeout(7); $LWP_Data->{feed} = $LWP_Data->{ua}->get($url); if ($LWP_Data->{feed}->is_success) { my $xml = $LWP_Data->{feed}->content; use Encode; my $encoded_xml = Encode::encode_utf8($xml); use XML::Simple qw(:strict); my $ref = XMLin( $encoded_xml, ForceArray => 1,KeyAttr => [ ]); # for a not nested hash ref foreach my $key (keys %{$ref }) { Encode::from_to($ref ->{$key}, "UTF-8", "iso-8859-1"); } ...

What I found in the forum is: Character Conversion Conundrum (https://perlmonks.org/?node_id=416914) which kind of implies XML::Simple converts to UTF-8 while it is not working when I only convert the results from utf-8 to iso-8859-1.

I am curious if what I assume is correct, if I might have read somewhere what I trialed and errored or if I am on the wrong track with my assumption.

Thanks for your feedback.
Regards
derion

In reply to XML::Simple and ISO-8859-1 encoding buggy? by derion

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.