You are here:  » export database as XML

Support Forum



export database as XML

Submitted by pixelcoder on Fri, 2007-08-17 11:41 in

Hi,

I saw the CSV example to export the pricetapestry database, is there an XML version as well?

Thanks in advance

Submitted by support on Fri, 2007-08-17 11:53

Hi,

There was a version posted a while back but it references some older include files that don't exist anymore, so here is an up to date version...

<?php
  
require("includes/common.php");
  
header("Content-Disposition: attachment; filename=products.xml");
  
header("Content-Type: text/xml");
  print 
"<products>";
  
$sql "SELECT * FROM `".$config_databaseTablePrefix."products`";
  if (
database_querySelect($sql,$rows))
  {
    
$baseHREF "http://".$_SERVER["HTTP_HOST"].$config_baseHREF;
    foreach(
$rows as $row)
    {
      print 
"<product>";
      if (
$config_useRewrite)
      {
        
$href $baseHREF "product/".tapestry_hyphenate($row["name"]).".html";
      }
      else
      {
        
$href $baseHREF "products.php?q=".urlencode($row["name"]);
      }
      print 
"<merchant>".$row["merchant"]."</merchant>";
      print 
"<productname>".$row["name"]."</productname>";
      print 
"<deeplink>".$href."</deeplink>";
      print 
"<price>".$row["price"]."</price>";
      print 
"<description>".$row["description"]."</description>";
      print 
"<image_url>".$row["image_url"]."</image_url>";
      print 
"<category>".$row["category"]."</category>";
      print 
"<brand>".$row["brand"]."</brand>";
      print 
"</product>";
    }
  }
  print 
"</products>";
?>

Hope this helps!
Cheers,
David.

Submitted by pixelcoder on Fri, 2007-08-17 12:08

that's great, thanks David.

Submitted by pixelcoder on Fri, 2007-08-17 12:14

ah ~ just a couple of things, firstly is there a way of leaving the XML feed on the server so it can be accessed directly rather then being downloaded [such as domain.com/feed.xml]

secondly I got a memory error 'Allowed memory size of 8388608 bytes exhausted (tried to allocate 82 bytes) in /public_html/includes/database.php'

Thanks again for the help.

Submitted by support on Fri, 2007-08-17 12:20

Hi,

The memory problem will be because you have a large database and there are too many products to return in a buffered query. The solution to this is to use an unbuffered query. I've incoporated this change with the modifications required to write the output to a file instead of being downloaded. To do this, create a directory called "export" within your Price Tapestry installation folder (although you will still run this script in the root directory). Make sure that this folder is writable by PHP (just give it global write permissions is the easiest way), and the script will then create products.xml in this directory...

<?php
  set_time_limit
(0);
  require(
"includes/common.php");
  
$f fopen("export/products.xml","w");
  if (!
$f) { print "Could not create export/products.xml! Check permissions...";exit(); }
  
fwrite($f,"<products>");
  
$sql "SELECT * FROM `".$config_databaseTablePrefix."products`";
  
$link mysql_connect($config_databaseServer,$config_databaseUsername,$config_databasePassword);
  
mysql_select_db($config_databaseName,$link);
  
$result mysql_unbuffered_query($sql,$link);
  while(
$row mysql_fetch_array($result,MYSQL_ASSOC))
  {
    
$baseHREF "http://".$_SERVER["HTTP_HOST"].$config_baseHREF;
    
$xml "";
    
$xml .= "<product>";
    if (
$config_useRewrite)
    {
      
$href $baseHREF "product/".tapestry_hyphenate($row["name"]).".html";
    }
    else
    {
      
$href $baseHREF "products.php?q=".urlencode($row["name"]);
    }
    
$xml .= "<merchant>".$row["merchant"]."</merchant>";
    
$xml .= "<productname>".$row["name"]."</productname>";
    
$xml .= "<deeplink>".$href."</deeplink>";
    
$xml .= "<price>".$row["price"]."</price>";
    
$xml .= "<description>".$row["description"]."</description>";
    
$xml .= "<image_url>".$row["image_url"]."</image_url>";
    
$xml .= "<category>".$row["category"]."</category>";
    
$xml .= "<brand>".$row["brand"]."</brand>";
    
$xml .= "</product>";
    
fwrite($f,$xml);
  }
  
fwrite($f,"</products>");
  print 
"Done.";
?>

Hope this helps!
Cheers,
David.

Submitted by pixelcoder on Fri, 2007-08-17 12:36

Thanks David, I've done as you said however I receive this error:

Warning: Invalid argument supplied for foreach() in /public_html/export.php on line 14

Any ideas?

Submitted by support on Fri, 2007-08-17 12:38

Ooops - sorry i've corrected the version above now - that loop is no longer required in the unbuffered version.

Cheers,
David.

Submitted by pixelcoder on Fri, 2007-08-17 13:24

thats perfect, working great now, thanks David.

Submitted by webie on Mon, 2007-08-20 23:13

Hi David,

Can we do this again but in csv or tab file?

Cheers

all the best

Darren

Submitted by support on Tue, 2007-08-21 07:45

Hi Darren,

There's some code for a CSV version in this thread:

http://www.pricetapestry.com/node/506

Cheers,
David.