----- Original Message ----- 
From: "Marc Boncz" <

Hi all,

A customer of mine has a website selling machines. Now he wants to advertise
his machines on another (generic) website. This site uses (sub)categories in
which products can be listed and then a link is given to a found product,
which may be from any of the companies supporting the generic site.

The generic site wants from me an XML page (say:
www.customer.nl/export.php), that they can download daily to update their
site. So far so good, because the XML is easy to format. Things complicate a
bit more as my customer has to provide the values of the (sub)category keys
with each article, in order for them to be processed correctly by the
generic site. Thus I need to implant in the site of my customer two tables,
for category and ubcategory, so on my site I can provide in the CMS two
dropdowns where the customer can select correctly the values for the foreign
keys as used in this other site. Having chosen those values, the XML I will
be exporting will be complete.

Now I need to build these tables. The generic site also has a script, which
can be called in cron, to give me the index (primary key) and value pairs I
need to consruct my dropdowns.

My problem is: they don't export these values as .csv format (which would be
muuuuuch easier, just read the response line by line, 'explode' the line,
and use the first value as primary key and the second as description).
Instead, they publish an XML file, in which values are given like


<row>
 <id>68</id>
 <cat_name>Aanvoer & aandrijftechniek</cat_name>
</row>

anybody who has a 'quick 'n dirty' hack for how to extract rapidly for each
row the values for 'id' and 'cat_name'?

Any parsing that goes beyond 'fgetcsv' and then 'explode' always seemed like
a huge waste of time to me when exporting csv is so much quicker and
simpler. But I guess even my time to start working on XML has come :-(

Marc
--------------------------
Quick and dirty! - you got it. Hand typed so please check!

$xml = // get the xml into a string here
$xml = str_replace("\r", '', $xml);
$xml = str_replace("\n", '', $xml);
$xml = str_replace("\t", '', $xml);
$xml = trim($xml);
$xml = str_replace('<row>', "\n<row>", $xml);
$xml = str_replace('</row>', "</row>\n", $xml);
$xml = explode("\n", $xml);
$index = 0;
foreach($xml as $oneline)
  {
  if (substr($oneline, 0, 5) != '<row>'){continue;}
  if(substr$($oneline, $strlen($oneline) - 6) != '</row>'){continue;}
  $block = substr($oneline, 5, $strlen($oneline) - 11);
  if(strpos($block, '<id>') === FALSE){continue;}
  if(strpos($block, '</id>') === FALSE){continue;}
  if(strpos($block, '<cat_name>') === FALSE){continue;}
  if(strpos($block, '</cat_name>') === FALSE){continue;}
  $idstart = strpos($block, '<id>');
  $idend = strpos($block, '</id>');
  $catstart = strpos($block, '<cat_name>');
  $catend = strpos($block, '</cat_name>');
  $id = substr($block, $idstart + 4, $idend - $idstart - 4);
  $cat = substr($block, $catstart + 10, $catend - $catstart - 10);
  $row[$index]['id'] = $id;
  $row[$index]['cat'] = $cat;
  $index++;
  }

if(count($row) > 0)
  {
  echo('<table border="1">' . 
"\n<tr>\n<th>Index</th><th>ID</th><th>Cat</th>\n</tr>\n");
  $index = 0;
  foreach($row as $thisrow)
    {
    echo("<tr>\n<td>" . $index . "</td><td>" . $thisrow['id'] . "</td><td>" 
. $thisrow['cat'] . "</td>\n</tr>\n");
    $index++;
    }
  echo("</table>\n");
  }

Assumes that the xml tags will always be lower case - ie quick and dirty

Thanks, Rob. 

Reply via email to