doc1355 Posted August 29, 2008 Share Posted August 29, 2008 Hi, Is there an easy way to convert a xml file to csv in php? I want to save the php to my server and process it with cron. Thanks Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 29, 2008 Share Posted August 29, 2008 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) :-\ Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 Yeah, why would you want to do that? Anyway, have a look at SimpleXML. The rest should be pretty simple, as far as I can tell. =P Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 29, 2008 Share Posted August 29, 2008 Yeah basically some loops through all the nodes and the values stored in an array, which in the end can be imploded by a ", ". Pretty easy Quote Link to comment Share on other sites More sharing options...
doc1355 Posted August 30, 2008 Author Share Posted August 30, 2008 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. Quote Link to comment Share on other sites More sharing options...
rarebit Posted August 30, 2008 Share Posted August 30, 2008 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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.