Page 1 of 1

creating xml file

Posted: Wed Feb 15, 2012 11:12 am
by narjis
I am retrieving records from the database and want to create an xml file from it. But this code is not working.Whee am I going wrong. Please check.

Code: Select all

<?php 
require("../sqlScripts/dbconnection.php");
//in this file I'm going to make xml foile of the database product  items
//////////////////////////////////////////////////////////////////////////
$sql = "SELECT * FROM products";
$result = mysql_query($sql);
// create a new XML document
$doc = new DomDocument();
  $doc->formatOutput = true;
// create root node
$root = $doc->createElement('products');
$root = $doc->appendChild($root);

// process one row at a time					

while($row = mysql_fetch_assoc($result)) {
	// add node for each row
  $occ = $doc->createElement('product');
  $occ = $root->appendChild($occ);
   // add a child node for each field
  print_r($row);
  foreach ($row as $fieldname => $fieldvalue) {
	//$child = $doc->createElement($fieldname);
	$new = $doc->createElement($fieldname);
	$new->appendChild($doc->createTextNode($fieldvalue));
  /*  $child = $occ->appendChild($child);
	$value = $doc->createTextNode($fieldvalue);
    $value = $child->appendChild($value);*/
	  } // foreach
	} // while
	
	// get completed xml document
$xml_string = $doc->saveXML();
 $doc->save("write2.xml");
echo $xml_string;
?>
It is giving the following error
Uncaught exception 'DOMException' with message 'Invalid Character Error' in C:\wamp\www\newOstore\administrator\product_xml_creation.php on line 23

Re: creating xml file

Posted: Wed Feb 15, 2012 3:40 pm
by social_experiment
https://bugs.php.net/bug.php?id=41484
What value are you trying to use when creating the element