Skip to content

Instantly share code, notes, and snippets.

@nitishn
Created February 9, 2015 21:47
Show Gist options
  • Save nitishn/0e8477b9592339cc2bc7 to your computer and use it in GitHub Desktop.
Save nitishn/0e8477b9592339cc2bc7 to your computer and use it in GitHub Desktop.
Read an XML file and write out a CSV file in php.
<?php
// How to open an XML file and write out it's contents as a CSV!
// Using sample XML file here https://msdn.microsoft.com/en-us/library/ms762271(d=printer,v=vs.85).aspx
$filexml='books.xml';
if (file_exists($filexml)) {
$xml = simplexml_load_file($filexml);
$f = fopen('books.csv', 'w');
foreach($xml->book as $book) {
$values = array(
'author' => $book->author,
'title' => $book->title,
);
fputcsv($f, $values,',','"');
}
fclose($f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment