0

我正在编写一个创建 xml 文件“settings.xml”的 perl 脚本。(使用 XML::Writer)。我希望文件以 UCS-2 大端编码,但我不确定如何。

我试过这样的事情:open(my $output, "> :encoding(UCS-2BE)", "settings.xml");,但所做的只是让文件输出一团糟,(例如http://i.imgur.com/p9cruCf.png或一系列汉字)同时保持编码文件为 ANSI。

知道如何解决这个问题,或者如何将文件转换为 UCS-2?

我是 Perl 的初学者,如果其中一些没有意义,我很抱歉。

编辑:对于遇到此问题的其他人,请参阅下面的答案,他们提供了有关如何解决此问题的详尽说明。

4

2 回答 2

2

XML::Writer 不支持除 US-ASCII 和 UTF-8 之外的任何内容(如其ENCODING构造函数参数的文档中所述)。使用 XML::Writer 创建 UCS-2be XML 文档很棘手,但并非不可能。

use XML::Writer qw( );

# XML::Writer doesn't encode for you, so we need to use :encoding.
# The :raw avoids a problem with CRLF conversion on Windows.
open(my $fh, '>:raw:encoding(UCS-2be)', $qfn)
   or die("Can't create \"$qfn\": $!\n");

# This prints the BOM. It's optional, but it's useful when using an
# encoding that's not a superset of US-ASCII (such as UCS-2be).
print($fh "\x{FEFF}");

my $writer = XML::Writer->new(
   OUTPUT   => $fh,
   ENCODING => 'US-ASCII',   # Use entities for > U+007F
);
$writer->xmlDecl('UCS-2be');
$writer->startTag('root');
$writer->characters("\x{00041}");
$writer->characters("\x{000C9}");
$writer->characters("\x{10000}");
$writer->endTag();
$writer->end();

缺点:U+007F 以上的所有字符都将显示为 XML 实体。在上面的例子中,

  • U+00041 将显示为 " A" ( 00 41)。好的。
  • U+000C9 将显示为 " É" ( 00 26 00 23 00 78 00 43 00 39 00 3B)。次优,但还可以。
  • U+10000 将显示为 " 𐀀" ( 00 26 00 23 00 78 00 31 00 30 00 30 00 30 00 30 00 3B)。很好,需要 XML 实体来存储 U+10000 和UCB-2e.

当且仅当您可以保证不会向作者提供任何高于 U+FFFF 的字符时,您才能避免上述缺点。

use XML::Writer qw( );

# XML::Writer doesn't encode for you, so we need to use :encoding.
# The :raw avoids a problem with CRLF conversion on Windows.
open(my $fh, '>:raw:encoding(UCS-2be)', $qfn)
   or die("Can't create \"$qfn\": $!\n");

# This prints the BOM. It's optional, but it's useful when using an
# encoding that's not a superset of US-ASCII (such as UCS-2be).
print($fh "\x{FEFF}");

my $writer = XML::Writer->new(
   OUTPUT   => $fh,
   ENCODING => 'UTF-8',   # Don't use entities.
);
$writer->xmlDecl('UCS-2be');
$writer->startTag('root');
$writer->characters("\x{00041}");
$writer->characters("\x{000C9}");
#$writer->characters("\x{10000}");  # This causes a fatal error
$writer->endTag();
$writer->end();
  • U+00041 将显示为 " A" ( 00 41)。好的。
  • U+000C9 将显示为 " É" ( 00 C9)。好的。
  • U+10000 导致致命错误。

以下是您可以在没有任何缺点的情况下做到这一点的方法:

use Encode      qw( decode encode );
use XML::Writer qw( );

my $xml;
{
   # XML::Writer doesn't encode for you, so we need to use :encoding.
   open(my $fh, '>:encoding(UTF-8)', \$xml);

   # This prints the BOM. It's optional, but it's useful when using an
   # encoding that's not a superset of US-ASCII (such as UCS-2be).
   print($fh "\x{FEFF}");

   my $writer = XML::Writer->new(
      OUTPUT   => $fh,
      ENCODING => 'UTF-8',   # Don't use entities.
   );
   $writer->xmlDecl('UCS-2be');
   $writer->startTag('root');
   $writer->characters("\x{00041}");
   $writer->characters("\x{000C9}");
   $writer->characters("\x{10000}");
   $writer->endTag();
   $writer->end();
   close($fh);
}

# Fix encoding.
$xml = decode('UTF-8', $xml);
$xml =~ s/([^\x{0000}-\x{FFFF}])/ sprintf('&#x%X;', ord($1)) /eg;
$xml = encode('UCS-2be', $xml);

open(my $fh, '>:raw', $qfn)
   or die("Can't create \"$qfn\": $!\n");

print($fh $xml);
  • U+00041 将显示为 " A" ( 00 41)。好的。
  • U+000C9 将显示为 " É" ( 00 C9)。好的。
  • U+10000 将显示为 " 𐀀" ( 00 26 00 23 00 78 00 31 00 30 00 30 00 30 00 30 00 3B)。很好,需要 XML 实体来存储 U+10000 和UCB-2e.
于 2013-05-23T19:45:32.187 回答
1

您没有描述出了什么问题,但是您可能遇到了一些 perl 版本在 Windows 上的错误,其中编码和 crlf 层之间的交互不良。如果是这样,这应该工作:

open(my $output, "> :raw:perlio:encoding(UCS-2BE):crlf:utf8", "settings.xml");

(有关说明,请参见http://www.perlmonks.org/?node_id=608532。)

如果没有,请提供比“所做的只是让文件输出一团糟”更多的信息。演示该问题的简短脚本会有所帮助。

于 2013-05-23T18:54:25.377 回答