Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

XML and HTML entities

7:07 pm on Jun 14, 2012 (gmt 0)

Preferred Member

10+ Year Member

joined:Dec 10, 2007
posts: 507
votes: 0

Hey all,

I have some problems with HTML entities in XML.

The XML file I have doesn't like having or & signs in it, or the HTML entity equivalent.

I have this to read the XML file:

$file = "stuff.xml";
// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");

Whenever I have a single or & sign in, it throws the unable to load file error. Remove the signs out completely and it displays the XML content fine.

I would of thought that simply using some PHP to convert the characters would be sufficient, but as it fails in loading the XML to begin with, this can't be done. Other than manually changing all the and & to XML friendly entities (but I dont know of one for either sign), is there any other solution?

6:26 pm on June 15, 2012 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
votes: 0

enclose the content in a <![CDATA[]]> node.

<![CDATA[ your $<%& funny characters go here ]]>

CDATA is "character data", it's intended as an enclosure for data that should not be parsed as XML.

It's a good practice to use a CDATA node to enclose any string data, especially if it's user-generated data like names, addresses, comments etc.

11:38 am on Dec 18, 2012 (gmt 0)

New User

joined:Oct 4, 2012
posts: 22
votes: 0

I was using the XHTML doctype is because the entities in my xml will be identical to xhtml, ie the nodes contain standard markup. As there are rather a lot I didn't want to specify them all, and as the main W3C doctypes come prebuilt into Firefox, the user wouldn't have to download them each time.