Dumb_Shock
Dumb_Shock

Reputation: 1008

making XML from database using PHP

I am Using the code below to make an VERY LARGE XML file from a database The code is not written by me its written by Jubba available at Kirupa

Well the problem is that everything is well formed but like around 30 '<' symbols in the output XML file are missing..so for example output would be something like this..

<XYZ> 
<ab>123333</ab> 
<resource>http://xxx.com</resource> 
/XYZ> /*  no '<'  */

The code used is:

header("Content-type: text/xml"); 

$host = "localhost"; 
$user = "root"; 
$pass = ""; 
$database = "test"; 

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host."); 
mysql_select_db($database, $linkID) or die("Could not find database."); 

 $query = "SELECT * FROM blog ORDER BY date DESC"; 
 $resultID = mysql_query($query, $linkID) or die("Data not found."); 

$xml_output = "<?xml version=\"1.0\"?>\n"; 
$xml_output .= "<entries>\n"; 

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){ 
$row = mysql_fetch_assoc($resultID); 
$xml_output .= "\t<entry>\n"; 
$xml_output .= "\t\t<date>" . $row['date'] . "</date>\n"; 
    // Escaping illegal characters 

$xml_output .= "\t\t<text>" . $row['text'] . "</text>\n"; 
$xml_output .= "\t</entry>\n"; 
} 

$xml_output .= "</entries>"; 

echo $xml_output; 

?>

Can u suggest me what the reason might be for the error??

Upvotes: 0

Views: 112

Answers (1)

Marc B
Marc B

Reputation: 360872

$row['text'] = str_replace("<", "<", $row['text']); 
                                 ^----- should be &lt;

Note the indicated character. You're basically doing a null-op here, producing invalid XML.

However, why not just use htmlspecialchars() for this? It encodes only the html (and XML) metacharacters as is (&'"<>)

Upvotes: 3

Related Questions