PHP中的UTF-8编码xml

can*_*boy 9 php xml utf-8

我正在尝试使用PHP输出XML,当我在Firefox中查看页面源时,一切似乎都很好.但是,页面本身无法正常显示.

在Firefox中,当它显示格式正确的XML时,它通常会在页面顶部显示:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
Run Code Online (Sandbox Code Playgroud)

..然后它显示了xml树.

但是,我只是获取xml标签中的字符,而没有树.

我能看到的唯一区别是我的页面编码为Western ISO-8859-1,正确显示的XML编码为Unicode UTF-8.那么如何将我的页面输出为Unicode UTF8?

我试过这个,但它没有任何区别:

echo utf8_encode($xmlstring);
Run Code Online (Sandbox Code Playgroud)

Gor*_*don 11

确保使用适当的内容类型和编码发送XML,例如

<?php header("Content-Type: application/xml; charset=utf-8"); ?>
Run Code Online (Sandbox Code Playgroud)

还要检查XML prolog是否包含正确的编码,例如

<?xml version="1.0" encoding="UTF-8"?>
Run Code Online (Sandbox Code Playgroud)

关于样式信息的消息指的是丢失的处理指令,例如

<?xml-stylesheet type="text/xsl" href="someting"?>
Run Code Online (Sandbox Code Playgroud)

这会告诉浏览器如何设置样式/格式化输出.如果您只想显示原始XML,则不一定需要它.