I'm trying to parse and modify some xml files unsing XML::DOM(::XPath). These xml files are encoded in UTF-8 and provide a correct xml declaration:
<?xml version = "1.0" encoding = "UTF-8"?>
If I save or print the modified xml, that declaration still is intact, however the actual file encoding changes to latin1. This even happens if I comment out all of the modifications and just parse the file. This reduces my code to:
#!/usr/bin/perl use XML::DOM::XPath; use strict; use warnings; my $parser= XML::DOM::Parser->new(); my $srcdir=shift; my $dstdir=shift; opendir(SRC,$srcdir); foreach(grep (/\.xml$/,readdir(SRC))) { my $doc = $parser->parsefile("$srcdir/$_"); $doc->printToFile("$dstdir/$_"); $doc->dispose; } close(SRC);
My knowledge of perl is rather basic, I must be doing something utterly wrong here but unfortunately, I can't figure out what it is. I'm using perl 5.8.8 and XML::DOM 1.43.

Any hints are appreciated; thank you in advance.


In reply to XML::DOM encodes in latin1 instead of UTF-8 by papo

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.