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)
The XML is most probably invalid.
The problem could be the "&"
$text=preg_replace('/&(?!#?[a-z0-9]+;)/g', '&', $text);
will get rid of the "&" and replace it with it's HTML code version...give it a try.
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&
). 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. <![CDATA[some text & some more text]]>
Note: ‘&’ ‘<' '>‘ will all give problems if not handled correctly.
Try to clean the HTML first using this function:
$html = htmlspecialchars($html);
Special chars are usually represented differently in HTML and it might be confusing for the compiler. Like &
becomes &
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With