2

I am generating xml file dynamically and getting error when i include newstext. Error: XML Parsing Error: undefined entity ! ERROR
By remove newstext xml generates perfectly. Here is the code i am using.

$sqlNews    =   "SELECT * FROM news";

$runSqlNews =   mysql_query($sqlNews);

while ($rowSqlNews  =   mysql_fetch_array($runSqlNews)) 
    $arrSqlNews[]   =   $rowSqlNews;


    header('Content-type: text/xml');
    header('Pragma: public');
    header('Cache-control: private');
    header('Expires: -1');
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

    echo '<xml>';

for($i=0;$i<count($arrSqlNews);$i++) 
{
    echo "<news>";
        echo "<newsId>".$arrSqlNews[$i][id]."</newsId>";
        echo "<newsAuthor>".$arrSqlNews[$i][news_author]."</newsAuthor>";
        echo "<newsText>".$arrSqlNews[$i][news_text]."</newsText>";
        echo "<description>".$arrSqlNews[$i][news_description]."</description>";
        echo "<image>".$arrSqlNews[$i][news_image]."</image>";
    echo "</news>";       
}
    echo '</xml>';

Hope my question is clear. Thanks in Advance !!

1
  • PHP has a couple of extensions to generate XML that take care of generating valid syntax... Commented Dec 12, 2012 at 11:42

2 Answers 2

2

Consider adding CDATA tags whenever you work with strings, as special characters can break your XML parse.

echo "<newsText><![CDATA[".$arrSqlNews[$i][news_text]."]]></newsText>";
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

echo "<newsText><![CDATA[".$arrSqlNews[$i][news_text]."]]></newsText>";

It will prevent the browser from parsing tags from that field content.

1 Comment

This thing working fine on my local server. When i upload to my site i got this error (XML Parsing Error: no element found). What might be the reason

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.