Jump to content

Convert XML to CSV using PHP


doc1355

Recommended Posts

I'm wandering why you need to convert a nicely structured xml to an ugly csv. Anyway, as I'm not good with these type of things, I asked my good ol' friend google and the only relevant result was this library that seems to get the job done, but it's commercial (commercial = damned)  :-\

I totally agree that XML looks much better than CSV. But the problem is I'm trying to upload the XML to my eCommerce store to update my products. The eCommerce site, accepts XML but the problem is that the format of the XML that the site is asking is based on CSV method. Let me show you the codes:

 

This is what I have:

<products>
  <product>
    <sku>10001</sku>
    <qty>2</qty>
    <price>822.51</price>
  </product>
</products>

 

Following is the XML format that my eCommerce site requires:

 

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:x2="http://schemas.microsoft.com/office/excel/2003/xml" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet">
  <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"></OfficeDocumentSettings>
  <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"></ExcelWorkbook>
  <Worksheet ss:Name="Sheet 1">
    <Table>
      <Row>
        <Cell>
          <Data ss:Type="String">sku</Data>
        </Cell>
	<Cell>
	  <Data ss:Type="String">qty</Data>
	</Cell>
	  <Cell>
          <Data ss:Type="String">price</Data>
        </Cell>
      </Row>
      <Row>
        <Cell>
          <Data ss:Type="String">n2610</Data>
        </Cell>
        <Cell>
          <Data ss:Type="Number">8</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">125.99</Data>
        </Cell>
      </Row>
    </Table>
  </Worksheet>
</Workbook>

 

I'm a beginner in php and XML. What I'm planning to do is to convert my xml version to my eCommerce xml version using PHP. If I do this, then I can add the php file to cron so that everything updates automatically.

 

I really appreciate if anyone could provide a small example about how to convert this. It really doesn't matter if I convert my XML to the second XML layout or CSV. I can use either one on me ecommerce site.

 

Thanks you guys for your answers.

 

 

I could swear there was more but I can't find the page I want... but look at:

http://uk2.php.net/manual/en/function.fputcsv.php

http://uk2.php.net/manual/en/function.fgetcsv.php

http://uk2.php.net/manual/en/function.str-getcsv.php

you just need to traverse your xml document...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.