PHP DOMDocument :: save()另存为ASCII而不是UTF-8 [英] PHP DOMDocument::save() saves as ASCII instead of UTF-8

查看:147
本文介绍了PHP DOMDocument :: save()另存为ASCII而不是UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DOMDocumentSimpleXMLElement创建格式化的XML文件.尽管所有这些都有效,但结果文件将另存为ASCII,而不是UTF-8.我找不到有关如何更改它的答案.

I'm using DOMDocument and SimpleXMLElement to create a formatted XML file. While this all works, the resulting file is saved as ASCII, not as UTF-8. I can't find an answer as to how to change that.

XML的创建方式如下:

The XML is created as so:

    $XMLNS = "http://www.sitemaps.org/schemas/sitemap/0.9";
    $rootNode = new \SimpleXMLElement("<?xml version='1.0' encoding='UTF-8'?><urlset></urlset>");
    $rootNode->addAttribute('xmlns', $XMLNS);

    $url = $rootNode->addChild('url');
    $url->addChild('loc', "Somewhere over the rainbow");

    //Turn it into an indented file needs a DOMDocument...
    $dom = dom_import_simplexml($rootNode)->ownerDocument;
    $dom->formatOutput = true;

    $path = "C:\\temp";

    // This saves an ASCII file
    $dom->save($path.'/sitemap.xml');

生成的XML看起来像这样(应该是我认为的):

The resulting XML looks like this (which is as it should be I think):

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>Somewhere over the rainbow</loc>
  </url>
</urlset>

不幸的是,该文件是ASCII编码的,而不是UTF-8.

Unfortunately the file is ASCII encoded and not UTF-8.

我该如何解决?

由于下面接受的答案,我现在可以正常工作了.有一个注意事项:我使用Notepad ++打开文件并检查编码.但是,当我重新生成文件时,Notepad ++会更新其选项卡,并且出于某种原因将ANSI指定为编码.在Notepad ++中关闭并重新打开同一文件将再次表示UTF-8.这使我感到困惑.

I've got it to work now thanks to the accepted answer below. There's one note: I used Notepad++ to open the file and check the encoding. However, when I re-generated the file, Notepad++ would update its tab and for some reason indicate ANSI as the encoding. Closing and reopening the same file in Notepad++ would then again indicate UTF-8 again. This caused me a load of confusion.

推荐答案

我认为这里发生了一些事情.首先,您需要:

I think there are a couple of things going on here. For one, you need:

$dom->encoding = 'utf-8';

但是,我认为我们应该尝试手动创建DOMDocument并指定正确的编码.所以:

But also, I think we should try creating the DOMDocument manually specifying the proper encoding. So:

<?php

$XMLNS = "http://www.sitemaps.org/schemas/sitemap/0.9";
$rootNode = new \SimpleXMLElement("<?xml version='1.0' encoding='UTF-8'?><urlset></urlset>");
$rootNode->addAttribute('xmlns', $XMLNS);

$url = $rootNode->addChild('url');
$url->addChild('loc', "Somewhere over the rainbow");

// Turn it into an indented file needs a DOMDocument...
$domSxe = dom_import_simplexml($rootNode)->ownerDocument;

// Set DOM encoding to UTF-8.
$domSxe->encoding = 'UTF-8';

$dom = new DOMDocument('1.0', 'UTF-8');
$domSxe = $dom->importNode($domSxe, true);
$domSxe = $dom->appendChild($domSxe);

$path = "C:\\temp";

$dom->formatOutput = true;
$dom->save($path.'/sitemap.xml');

还要确保您添加的任何元素或CData实际上都是UTF-8(请参见 utf8_encode() ).

Also ensure that any elements or CData you're adding are actually UTF-8 (see utf8_encode()).

使用上面的示例,这对我有效:

Using the example above, this works for me:

php > var_dump($utf8);
string(11) "ᙀȾᎵ⁸"

php > $XMLNS = "http://www.sitemaps.org/schemas/sitemap/0.9";
php > $rootNode = new \SimpleXMLElement("<?xml version='1.0' encoding='UTF-8'?><urlset></urlset>");
php > $rootNode->addAttribute('xmlns', $XMLNS);
php > $url = $rootNode->addChild('url');

php > $url->addChild('loc', "Somewhere over the rainbow $utf8");

php > $domSxe = dom_import_simplexml($rootNode);
php > $domSxe->encoding = 'UTF-8';
php > $dom = new DOMDocument('1.0', 'UTF-8');
php > $domSxe = $dom->importNode($domSxe, true);
php > $domSxe = $dom->appendChild($domSxe);
php > $dom->save('./sitemap.xml');


$ cat ./sitemap.xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>Somewhere over the rainbow ᙀȾᎵ⁸</loc></url></urlset>

这篇关于PHP DOMDocument :: save()另存为ASCII而不是UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆