如何在 Perl 中将 XML 文档从 Latin-1 转换为 UTF-8?

发布于 2024-08-09 05:23:33 字数 231 浏览 11 评论 0原文

我们公司希望将我们托管的所有站点从 Latin-1 转换为 UTF-8。经过大量谷歌搜索后,我们的 Perl 脚本几乎完成了。现在唯一缺少的是 XML 文件。

将 XML 从 Latin-1 转换为 UTF-8 的最佳方法是什么?它有用吗?

我问这个问题是因为我们对此不确定,因为谷歌上的大多数条目都解释了如何做完全相反的事情。甚至有人说utf8可能会导致XML出现问题。您能给我们介绍一下整个 XML 编码问题吗?

We at the company want to convert all the sites we are hosting from Latin-1 to UTF-8. After a ot of googling, we have our Perl script almost complete. The only thing that is missing now are the XML files.

What is the best way to convert XML from Latin-1 to UTF-8 and is it useful?

I am asking because we are unsure about it since most entries on Google explain how to do the exact opposite. Some even say that utf8 may cause problems with XML. Can you enlighten us on the whole XML Encoding Issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

誰認得朕 2024-08-16 05:23:33

你要转换什么?数据、XML 标签还是其他什么?

我认为你只需要将其读取为 Latin-1 并将其重写为 UTF-8 除非你的源代码做了一些非常奇怪的事情。解码和编码发生在文件句柄级别。一旦你在 Perl 中使用它,它的内部就已经是 UTF-8 了。

到目前为止你有什么?您遇到什么问题?

您的情况是否太复杂而无法仅使用 xmllint

 xmllint --encode utf8 --output filename.xml filename.xml.latin1

如果您使用的是 XML::Parser,请参阅有关该模块的 Juerd 的 Unicode 建议

如果您要转换的不仅仅是 XML 文件,iconv可能有帮助:

iconv -f ISO-8859-1 -t UTF-8 filename.txt.latin1 > filename.txt

What are you converting? The data or the XML tags or something else?

I think you just need to read it as Latin-1 and rewrite it as UTF-8 unless your source does something really weird. The decoding and encoding happens for you at the filehandle level. Once you have it in Perl, it's internally UTF-8 already.

What do you have so far? What problems are you having?

Is your situation too complicated to merely use xmllint?

 xmllint --encode utf8 --output filename.xml filename.xml.latin1

If you are using XML::Parser, see Juerd's Unicode Advice about that module.

If you are converting more than just XML files, iconv might help:

iconv -f ISO-8859-1 -t UTF-8 filename.txt.latin1 > filename.txt
千寻… 2024-08-16 05:23:33

我会使用xmllint --encode utf8 FILE-NAME,示例:

xmllint --encode utf8 --output test.xml test.xml

将正确地将test.xml(无论它可能有什么编码)转换为UTF-8,包括XML序言。

I'd use xmllint --encode utf8 FILE-NAME, sample:

xmllint --encode utf8 --output test.xml test.xml

will correctly convert test.xml (whatever encoding it may have) to UTF-8 including the XML prologue.

爱的十字路口 2024-08-16 05:23:33

正如 brian 提到的,Perl 内部使用 UTF-8。不管你想要与否,Perl 都会转换它。

这个诡计与 UTF8 标志有关,该标志是附加到每个字符串的位标志。对于 XML::Parser 返回的数据,设置了 UTF8 标志。

如果您想摆脱此行为,请清除 UTF8 标志。一种方法是这样的:

sub de_utf8 {
    use bytes;
    return "$_[0]";
}

这样,结果字符串将是与原始字符串相同的字节数据。

编辑:有点偏离OP的主题...抱歉。

As brian mentioned its internally UTF-8 in Perl. Perl will convert it whether you want it or not.

The trickery is connected to the UTF8 flag, which is a bit flag attached to each string. For the data that XML::Parser returns, that UTF8 flag is set.

If ever you want ot get rid of this behaviour, clear the UTF8 flag. One way you can do it, is like this:

sub de_utf8 {
    use bytes;
    return "$_[0]";
}

This way, the resulting string will be the same byte data as the original string.

EDIT: A bit off the topic of the OP... sorry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文