in reply to XML:: DOM and Accented Characters
use strict; use warnings; use XML::DOM; my $xml = <<"__EOI__"; <?xml version="1.0" encoding="UTF-8"?> <TEST> \xC3\xA9 </TEST> __EOI__ my $parser = new XML::DOM::Parser; my $doc = $parser->parse($xml); $doc->printToFile("test.xml");
>perl a.pl >perl -e"$/=\16; while (<>) { my $s=uc unpack 'H*', $_; $s=~s/..\K/ /g +; print qq{$s\n}; }" test.xml 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E 3D 22 31 2E 30 22 20 65 6E 63 6F 64 69 6E 67 3D 22 55 54 46 2D 38 22 3F 3E 0A 3C 54 45 53 54 3E 20 E9 20 3C 2F 54 45 53 54 3E 0A
As previously shown, XML::DOM doesn't encode for you (as it should). So let's try with the previously mentioned fix:
use strict; use warnings; use XML::DOM; my $xml = <<"__EOI__"; <?xml version="1.0" encoding="UTF-8"?> <TEST> \xC3\xA9 </TEST> __EOI__ my $parser = new XML::DOM::Parser; my $doc = $parser->parse($xml); open my $fh, ">:utf8", "test.xml" or die $!; $doc->printToFileHandle($fh);
>perl a.pl >perl -e"$/=\16; while (<>) { my $s=uc unpack 'H*', $_; $s=~s/..\K/ /g +; print qq{$s\n}; }" test.xml 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E 3D 22 31 2E 30 22 20 65 6E 63 6F 64 69 6E 67 3D 22 55 54 46 2D 38 22 3F 3E 0A 3C 54 45 53 54 3E 20 C3 A9 20 3C 2F 54 45 53 54 3E 0A
Perl did its thing correctly, so you have a problem with your editor. There are some solutions:
Tell your editor the file is encoded using UTF-8 through its menus.
Add a BOM. Most editors use this ass a signal that the file is encoded using UTF-8.
open my $fh, ">:utf8", "test.xml" or die $!; print($fh "\x{FEFF}"); $doc->printToFileHandle($fh);
Use the encoding the editor expects (cp1252?)
...Fix the <?xml?> line... open my $fh, ">:encoding(cp1252)", "test.xml" or die $!; $doc->printToFileHandle($fh);
You might want to check (using the above command) to make sure your input contains what you think it contains.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: XML:: DOM and Accented Characters
by freeflyer (Novice) on Aug 09, 2010 at 09:53 UTC | |
by ikegami (Patriarch) on Aug 09, 2010 at 13:51 UTC | |
by freeflyer (Novice) on Aug 09, 2010 at 15:54 UTC |