问题描述
我正在尝试使用PHP输出XML,当我在Firefox中查看页面源代码时,一切似乎都很好.但是,页面本身无法正确显示.
I'm trying to output XML using PHP, and when I look at the page source in Firefox everything seems fine. However, the page itself doesn't display properly.
在Firefox中,当它显示正确格式的XML时,通常会在页面顶部显示此信息:
In Firefox, it usually says this at the top of the page when it's showing correctly formatted XML:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
..然后显示xml树.
..and then it shows the xml tree.
但是,我只是在xml标记中获得了字符,而没有树.
However, I just get the characters within the xml tags, and no tree.
我能看到的唯一区别是我的页面被编码为Western ISO-8859-1,而正确显示的XML被编码为Unicode UTF-8.那么如何将页面输出为Unicode UTF8?
The only difference I can see is that my page is encoded as Western ISO-8859-1 and the correctly displayed XML is encoded as Unicode UTF-8. So how would I output my page as Unicode UTF8?
我已经尝试过了,但这没什么区别:
I've tried this but it doesn't make any difference:
echo utf8_encode($xmlstring);
推荐答案
请确保您发送的XML具有适当的内容类型和编码,例如
Make sure you send the XML with an appropriate content-type and encoding, e.g.
<?php header("Content-Type: application/xml; charset=utf-8"); ?>
还要检查XML序言是否包含正确的编码,例如
Also check that the XML prolog contains the proper encoding, e.g.
<?xml version="1.0" encoding="UTF-8"?>
有关样式信息的消息是指缺少的处理指令,例如
The message about the style information refers to a missing processing instruction, e.g.
<?xml-stylesheet type="text/xsl" href="someting"?>
,它将告诉浏览器如何设置输出的样式/格式.如果您只想显示原始XML,则不一定需要.
which would tell the browser how to style/format the output. It is not necessarily needed if you just want to display the raw XML.
这篇关于PHP中的UTF-8编码xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!