Welcome to WebmasterWorld Guest from 54.145.221.99

Forum Moderators: httpwebwitch

Message Too Old, No Replies

XML and HTML entities

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

5+ Year Member



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?

Thanks.
6:26 pm on Jun 15, 2012 (gmt 0)

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



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.

[w3schools.com...]
11:38 am on Dec 18, 2012 (gmt 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.