Vignesh Gopalakrishnan
Vignesh Gopalakrishnan

Reputation: 1982

Generating XML Dynamically in PHP - Parsing Error

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 !!

Upvotes: 2

Views: 523

Answers (2)

MarcinJuraszek
MarcinJuraszek

Reputation: 125610

Try this:

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

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

Upvotes: 1

Dave Hart
Dave Hart

Reputation: 347

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>";

Upvotes: 2

Related Questions