I want to create an xml file from a database, and then to echo this file to use in another page for some ajax.
this is the code I use to create the xml file :
<?php
header("Content-Type:text/XML");
$connexion = new PDO("mysql:host=localhost;dbname=produits;charset=utf8", 'root', 'toor');
$statement = $connexion->prepare("SELECT * FROM produit");
$statement->execute();
$resultats = $statement->fetchAll(PDO::FETCH_OBJ);
$xml_file = new DOMDocument('1.0');
$root = $xml_file->createElement('produits');
foreach($resultats as $produit){
$tel = $xml_file->createElement('telephone');
$tel->setAttribute('id', $produit->id);
$marque = $xml_file->createElement('marque');
$marqueText = $xml_file->createTextNode($produit->marque);
$marque->appendChild($marqueText);
$tel->appendChild($marque);
$model = $xml_file->createElement('model');
$modelText = $xml_file->createTextNode($produit->model);
$model->appendChild($modelText);
$tel->appendChild($model);
$prix = $xml_file->createElement('prix');
$prix->setAttribute("devise", "DH");
$prixText = $xml_file->createTextNode($produit->prix);
$prix->appendChild($prixText);
$tel->appendChild($prix);
$root->appendChild($tel);
}
$xml_file->appendChild($root);
echo $xml_file;
?>
The problem is when I open the page to check if the xml file is create, I get this error message :
XML Parsing Error: junk after document element Location:
http://localhost/Ajax/produits/details.php
Line Number 2, Column 1: ^
How can I solve this problem ?