You are here:  » How to export xml data to csv from an Url-List??


How to export xml data to csv from an Url-List??

Submitted by umbro909 on Sun, 2007-09-09 14:08 in

Hi David,

Since our previous exchange, because I cant import xmldata into mysql, I'm trying another way...

I want to export xmldata to a file.csv with this code:

<?php
  
require("MagicParser.php");
  function 
myProductRecordHandler($record)
  {
    global 
$fp;
    
// fwrite($fp,implode(";",$record)."\n");
    
fwrite($fp,$record["PRODUCT/PRODUCTDESCRIPTION-ID"].";".$record["PRODUCT/PRODUCTDESCRIPTION-SHORTDESC"].";".$record["PRODUCT/PRODUCTDESCRIPTION-LONGDESC"]."\n");
  }
  
$filename "xmlexport".".csv";
  
$fp fopen($filename,"a");
  if (!
$fp)
  {
    print 
"error message if no.csv - check it";exit();
  }
  
$url="http://localhost/magic/pathxml/42211.xml";
  
MagicParser_parse($url,"myProductRecordHandler","xml|INTERFACE/");
  
fclose($fp);
?>

For that, everything works fine!
But I need to parse the hole "pathxml" directory who contains +- 20000 files.xml

I found this correct script to list the files of this directory, but I cant mix these 2 script together:

<?php
if ($handle opendir('../magic/pathxml')) {
    while (
false !== ($file readdir($handle))) {
        if (
$file != "." && $file != "..") {
            echo 
"$file\n";
        }
    }
    
closedir($handle);
}
?>

Or if I create a urllist.txt, is there a way to parse it with something like :$urlList="http://localhost/magic/pathxml/urllist.txt";

Cheers
Umbro ;-)

Submitted by support on Sun, 2007-09-09 15:09

Hello Umbro,

These scripts should mix together quite easily! Here is what I think should work, but the only think
I am not sure about is the paths. I am assuming that your first script is in the directory "magic",
so on that basis, try this:

<?php
  
require("MagicParser.php");
  function 
myProductRecordHandler($record)
  {
    global 
$fp;
    
// fwrite($fp,implode(";",$record)."\n");
    
fwrite($fp,$record["PRODUCT/PRODUCTDESCRIPTION-ID"].";".$record["PRODUCT/PRODUCTDESCRIPTION-SHORTDESC"].";".$record["PRODUCT/PRODUCTDESCRIPTION-LONGDESC"]."\n");
  }
  
$filename "xmlexport".".csv";
  
$fp fopen($filename,"a");
  if (!
$fp)
  {
    print 
"error message if no.csv - check it";exit();
  }
  if (
$handle opendir('pathxml')) {
    while (
false !== ($file readdir($handle))) {
        if (
$file != "." && $file != "..") {
           print 
"Parsing: ".$file."\n";
           
MagicParser_parse("pathxml/".$file,"myProductRecordHandler","xml|INTERFACE/");
        }
    }
    
closedir($handle);
  }
  
fclose($fp);
?>

Notice how the filename parameter to MagicParser_parse is "pathxml/".$file - this is what you will have
to check if I am not right about this script being in the "magic" directory...!

Hope this helps!
Cheers,
David.

Submitted by umbro909 on Sun, 2007-09-09 16:31

One word... Perfect!

But as you know, I'm a little complicated man ;-)..

How can I include this following code inside the rest of the code, because I need to export this too ;-) :

<?php
  header
("Content-Type: text/html;charset=utf-8");
  require(
"MagicParser.php");
print 
'<link href="css/fle.css" rel="stylesheet" type="text/css">';
$url="http://localhost/magic/pathxml/".$_GET['doc'];
  
// Build Array of CategoryFeatureGroup Names
  
$featureGroup = array();
  function 
myCategoryFeatureGroupRecordHandler($record)
  {
    global 
$featureGroup;
    
$id $record["CATEGORYFEATUREGROUP-ID"];
    
$name $record["FEATUREGROUP/NAME-VALUE"];
    
$featureGroup[$id]["name"] = $name;
    
$featureGroup[$id]["features"] = array();
  }
  
MagicParser_parse($url,"myCategoryFeatureGroupRecordHandler","xml|INTERFACE/PRODUCT/CATEGORYFEATUREGROUP/");
  
// Build Sub-Arrays of Features beneath featureGroup array
  
function myProductFeatureRecordHandler($record)
  {
    global 
$featureGroup;
    
$featureGroupID $record["PRODUCTFEATURE-CATEGORYFEATUREGROUP_ID"];
    
$name $record["FEATURE/NAME-VALUE"];
    
$value $record["PRODUCTFEATURE-VALUE"];
    
$measure $record["FEATURE/MEASURE-SIGN"];
    
$featureGroup[$featureGroupID]["features"][] = array("key"=>$name,"value"=>$value.$measure);
  }
  
MagicParser_parse($url,"myProductFeatureRecordHandler","xml|INTERFACE/PRODUCT/PRODUCTFEATURE/");
  
/***************************/
  /* Print Features
  /***************************/
   
print "<table>";
  foreach(
$featureGroup as $fg)
  {
    
// we only want to display the feature groups that have associated features
    
if (count($fg["features"]))
    {
    print 
"<table class='border' cellspacing='0px'>";
    print 
"<tr>";
    print 
"<th class='title'>".$fg["name"]."</th>";
        print 
"</tr>";
      foreach(
$fg["features"] as $feature)
      {
        print 
"<tr>";
        print 
"<th class='label'>".$feature["key"]." :</th>";
        print 
"<td class='data'>".$feature["value"]."</td>";
        print 
"</tr>";
      }
      print 
"<table>";
    }
  }
  print 
"</table>";
?>

Have you an Idea??

And thanks again for all you've done for us (US= LITTLE NOVICE) ;-)

Submitted by support on Sun, 2007-09-09 16:42

Hello Umbro,

There's quite a lot of code in the second script.

What part of it do you need to combine?

Are you trying to do the same thing, exporting to CSV?

Cheers,
David.