Outputting from PHP to XML file

Cache-man

Wannabe Webmaster
Joined
Mar 16, 2005
Messages
840
Reaction score
0
OK, I've sussed how to connect to the MySQL database using PHP, and have done some queries in order to pull some products from a shop, which is half of my current project sussed :)

Now from this stage I'd like to be able to output this generated information into a XML file. Does anyone know how/if this is possible? If so, could you give me some pointers.

The code I've done so far to pull the products list and output it to the browser is as follows
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Our List of Products</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
// Connect to the database server
$dbcnx = @mysql_connect('localhost','bigblues_test','***');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' .
'database server at this time.</p>');
}
// Select the products database
if (!@mysql_select_db('bigblues_store')) {
exit('<p>Unable to locate the joke ' .
'database at this time.</p>');
}
?>
<h1>MySQL & PHP Integration Test</h1>
<h2>Here are all the products in our database:</h2>
<blockquote>
<?php
// Request the name of all the products
$result = @mysql_query('SELECT product, descr, fulldescr, list_price, MID(image_path, 2) AS image_path FROM xcart_products p INNER JOIN xcart_images_T i ON p.productid = i.id');
if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
// Display the text of each product in a paragraph
while ($row = mysql_fetch_array($result)) {
echo '<h3><u>' . $row['product'] . '</u></h3>';
echo '<p>' . $row['descr'] . '<br />';
echo $row['fulldescr'] . '<br />';
echo '<img src="http://www.bigbluesheep.com/xcart' . $row['image_path'] . '" /><br />';
echo '<strong>£' . $row['list_price'] . '</strong></p>';
echo '<hr />';
}
?>
</blockquote>
</body>
</html>
Which generates the results shown here http://bigbluesheep.com/xcart/test.php

Now how do I export that to a XML file instead of the browser.
The XML schema will look something like this
PHP:
<allitems>
  <item>
	<name></name>
	<description></description>
	<description2></description2>
	<price></price>
	<image></image>
  </item>
</allitems>
 
Last edited:

Cache-man

Wannabe Webmaster
Joined
Mar 16, 2005
Messages
840
Reaction score
0
Well, I've managed to get the data outputting to XML in the browser but CAN NOT find any way to output to a file.
Here is the code I've got so far to get XML on the screen:
PHP:
<?php
// Connect to the database server
$dbcnx = @mysql_connect('localhost','bigblues_test','***');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' .
'database server at this time.</p>');
}
// Select the products database
if (!@mysql_select_db('bigblues_store')) {
exit('<p>Unable to locate the joke ' .
'database at this time.</p>');
}
?>
<?php

// Request the name of all the products
$result = @mysql_query('SELECT product, descr, fulldescr, list_price, MID(image_path, 2) AS image_path FROM xcart_products p INNER JOIN xcart_images_T i ON p.productid = i.id LIMIT 0,30');
if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
header("Content-Type: text/xml");
echo("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
echo("<allitems>\n");
// Display the text of each product in a paragraph
while ($row = mysql_fetch_array($result)) {
echo '<item>';
echo '<name>' . $row['product'] . '</name>';
echo '<description>' . $row['descr'] . '</description>';
echo '<description2>' . $row['fulldescr'] . '</description2>';
echo '<image>http://www.bigbluesheep.com/xcart' . $row['image_path'] . '</image>';
echo '<price>' . $row['list_price'] . '</price>';
echo '</item>';
}
echo '</allitems>';
?>
The output can be seen here http://bigbluesheep.com/xcart/test-xml.php

Now any ideas how to output it to a XML file???
 

Cache-man

Wannabe Webmaster
Joined
Mar 16, 2005
Messages
840
Reaction score
0
YAYY. I did it, after a lot of googling dubious pages.
Here is my solution.
PHP:
<?php
// Connect to the database server
$dbcnx = @mysql_connect('localhost','bigblues_test','***');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' .
'database server at this time.</p>');
}
// Select the products database
if (!@mysql_select_db('bigblues_store')) {
exit('<p>Unable to locate the ' .
'database at this time.</p>');
}
?>

<?php
// Request the name of all the products
$result = @mysql_query('SELECT product, descr, fulldescr, list_price, MID(image_path, 2) AS image_path FROM xcart_products p INNER JOIN xcart_images_T i ON p.productid = i.id LIMIT 0,30');
if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
?>

<?php
// create a new XML document
$doc = new DomDocument('1.0');
// create root node
$allitems = $doc->createElement('allitems');
$allitems = $doc->appendChild($allitems);
// process one row at a time
while($row = mysql_fetch_assoc($result)) {
// add node for each row
$item = $doc->createElement('item');
$item = $allitems->appendChild($item);  
// add a child node for each field
foreach ($row as $fieldname => $fieldvalue) {  
$child = $doc->createElement($fieldname);
$child = $item->appendChild($child);	
$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value); 
  } // foreach
} // while
// get completed xml document
$xml_string = $doc->saveXML();
$doc->save("testing123.xml");
echo '<h1>Products exported to XML file</h1>';
echo '<h3><a href="testing123.xml">Click here to go to the testing123.xml file.</a></h3>'


?>
Works well enough for me, and heres what happens http://bigbluesheep.com/xcart/test-xml2.php , then you just click the link to view the generated xml.

I'd still welcome any comments or suggestions as to how the code could be any better though, as this is my first attempt at anything like this.
 

Ian

Administrator
Joined
Feb 23, 2002
Messages
19,873
Reaction score
1,499
Good work :D

I saw this thread first thing this morning before uni and just came back to have a bash at solving it. Nice clean code there, I never comment my PHP and it always backfires on me and I wish I had! ;)
 

Cache-man

Wannabe Webmaster
Joined
Mar 16, 2005
Messages
840
Reaction score
0
If I'm honest Ian, a lot of it is copied and pasted from various articles and discussions, and thencustomised line by line for my needs.
As for comments I always try to use them, especially if learning and testing stuff like this. I would probabally strip a couple of them out for publishing a live version, but I'd leave the main ones there for my own benefit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top