11

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);
1
  • Show us the source. Does it have the <?xml... ?> header? Commented Nov 12, 2010 at 12:45

1 Answer 1

14

Make sure you send the XML with an appropriate content-type and encoding, e.g.

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

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"?>

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I can also just add header("Content-Type: text/xml;"); and it seems to take on UTF-8 encoding by default.
Best answer to this question from @andyk stackoverflow.com/a/486786/757850
i add this <?xml version="1.0" encoding="UTF-8"?> but its the same problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.