note 95447 added to function.simplexml-load-string

From: Date: Mon, 04 Jan 2010 07:40:23 +0000
Subject: note 95447 added to function.simplexml-load-string
Groups: php.notes 
Request: Send a blank email to php-notes+get-164581@lists.php.net to get a copy of this message
I had a heck of a time getting a small XML-based CMS to work. My pages looked like this <page> <title>Example</title> <content> <div style="blah.."> Valid XHTML goes here </div> </content> </page> but everytime I ran simplexml_load_string() on my XML data, I got errors like: Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 15: parser error : Entity 'nbsp' not defined in /var/www/dev... I searched around and found no information except for a bogus bug report in bugs.php.net that explained nothing. I found two solutions: Solution #1: use LIBXML_DTDLOAD in the options and define the XHTML doctype, e.g. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> Although that will require going out and loading the DTD each page load. Pretty poor. To remedy this I just copied the XHTML DTD locally and used it like so: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "xml/dtd/xhtml1-strict.dtd"> If you have the DTD and related files in xml/dtd/ .. For the XHTML Strict DTD there are 4 files total, you have to get all of them locally. The files are: xhtml-lat1.ent, xhtml-special.ent, xhtml-symbol.ent, xhtml1-strict.dtd Or wait, solution #2 (hacky but FASTER): <?php $xml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOERROR); ?> I developed the site with solution #1 but once I went live I switched it to the second solution I gave. Also, another quick note, to combine the options AFAIK, use | not & like I assumed. I don't know much about bitwise, but this worked for me when combining two options: <?php $xml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_COMPACT); ?> Thanks, Ben Carroll @ http://www.netsupportchat.com ---- Server IP: 208.69.120.55 Probable Submitter: 67.236.74.94 ---- Manual Page -- http://www.php.net/manual/en/function.simplexml-load-string.php Edit -- https://master.php.net/note/edit/95447 Del: integrated -- https://master.php.net/note/delete/95447/integrated Del: useless -- https://master.php.net/note/delete/95447/useless Del: bad code -- https://master.php.net/note/delete/95447/bad+code Del: spam -- https://master.php.net/note/delete/95447/spam Del: non-english -- https://master.php.net/note/delete/95447/non-english Del: in docs -- https://master.php.net/note/delete/95447/in+docs Del: other reasons-- https://master.php.net/note/delete/95447 Reject -- https://master.php.net/note/reject/95447 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#164581) next »