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)  :-\

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.