Thursday, July 30, 2015

'xmlParseEntityRef: no name' warnings while loading xml into a php file

I am reading an xml in php using simplexml_load_file. However while trying to load the xml it displays a list of warnings
Warning: simplexml_load_file() [function.simplexml-load-file]: <project orderno="6" campaign_name="International Relief & Development" project in /home/bluecard1/public_html/test.php on line 3    
Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /home/bluecard1/public_html/test.php on line 3    
Warning: simplexml_load_file() [function.simplexml-load-file]: http://..../index.php/site/projects/:15: parser error : xmlParseEntityRef: no name in /home/bluecard1/public_html/test.php on line 3

Warning: simplexml_load_file() [function.simplexml-load-file]: ional Relief & Development" project_id="313" client_name="International Relief & in /home/bluecard1/public_html/test.php on line 3    
Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /home/bluecard1/public_html/test.php on line 3    
Warning: simplexml_load_file() [function.simplexml-load-file]: http://..../index.php/site/projects/:15: parser error : xmlParseEntityRef: no name in /home/bluecard1/public_html/test.php on line 3
How do I rectify to remove these warnings?
(XML is generated from url http://..../index.php/site/projects & loaded into a variable in the test.php. I dont have write priveleges to index.php)
shareimprove this question
   
The XML is invalid. You might not be able to load it at all. Errors can be suppressed by adding @ in front ofsimplexml_load_file or by adding a flag, see the manual page of simplexml_load_file for more information and please delete your question, it's a duplicate. –  hakre Sep 29 '11 at 23:54
   
I can see that my answer is getting quite a lot of attention, if that's actually the solution: can you please flag it as "correct answer"? thanks. –  ricricucit May 13 '14 at 12:12

4 Answers

The problem could be the "&"
$text=preg_replace('/&(?!#?[a-z0-9]+;)/', '&amp;', $text);
will get rid of the "&" and replace it with it's HTML code version...give it a try.
shareimprove this answer
Found this here ...
Problem: An XML parser returns the error “xmlParseEntityRef: noname”
Cause: There is a stray ‘&’ (ampersand character) somewhere in the XML text eg. some text & some more text
Solution:
  • Solution 1: Remove the ampersand.
  • Solution 2: Encode the ampersand (that is replace the '&' character with '& amp;' ). Remember to Decode when reading the XML text.
  • Solution 3: Use CDATA sections (text inside a CDATA section will be ignored by the parser.) eg.
Note: ‘&’ ‘<' '>‘ will all give problems if not handled correctly.
shareimprove this answer
3 
This saved me today. –  Bwire Jun 16 '14 at 10:19
The XML is invalid.
<![CDATA[ 
{INVALID XML}
]]> 
CDATA should be wrapped around all special XML characters, as per W3C
shareimprove this answer
This is in deed due to characters messing around with the data. Using htmlentities($yourText)worked for me (I had html code inside the xml document). See http://uk3.php.net/htmlentities.
shareimprove this answer

Your Answer

http://stackoverflow.com/questions/7604436/xmlparseentityref-no-name-warnings-while-loading-xml-into-a-php-file