Created
February 9, 2015 21:47
-
-
Save nitishn/0e8477b9592339cc2bc7 to your computer and use it in GitHub Desktop.
Read an XML file and write out a CSV file in php.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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