简体   繁体   中英

UTF-8 encoding xml in PHP

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.

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.

..and then it shows the xml tree.

However, I just get the characters within the xml tags, and no tree.

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);

Make sure you send the XML with an appropriate content-type and encoding, eg

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

Also check that the XML prolog contains the proper encoding, eg

<?xml version="1.0" encoding="UTF-8"?>

The message about the style information refers to a missing processing instruction, eg

<?xml-stylesheet type="text/xsl" href="someting"?>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM