0

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

1 Answer 1

3
$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 (&'"<>)

Sign up to request clarification or add additional context in comments.

Comments

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.