Last active
May 12, 2022 18:22
-
-
Save amityweb/479a0ca0e5ef39a786bc to your computer and use it in GitHub Desktop.
Revisions
-
Laurence Cope revised this gist
Sep 16, 2015 . 1 changed file with 6 additions and 31 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,37 +1,12 @@ <?php // Map CSV file to array $rows = array_map('str_getcsv', file('data.csv')); $header = array_shift($rows); $data = array(); foreach ($rows as $row) { $data[] = array_combine($header, $row); } // Process Data if need be -
Laurence Cope revised this gist
Sep 16, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -62,7 +62,7 @@ } // Set the formatOutput attribute of xml to true $xml->formatOutput = true; // Output to screen -
Laurence Cope revised this gist
Sep 16, 2015 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -43,11 +43,11 @@ //Creates XML string and XML document using the DOM $xml = new DomDocument('1.0', 'UTF-8'); //Add root node $root = $xml->createElement('root'); $xml->appendChild($root); // Add child nodes foreach($data AS $key => $val) { $entry = $xml->createElement('entry'); @@ -62,7 +62,8 @@ } // Set the formatOutput attribute of dom to true $xml->formatOutput = true; // Output to screen //header('Content-Type: text/xml'); -
Laurence Cope revised this gist
Sep 16, 2015 . 1 changed file with 3 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -56,15 +56,9 @@ foreach($val AS $field_name => $field_value) { $field_name = preg_replace("/[^A-Za-z0-9]/", '', $field_name); // preg_replace has the allowed characters $name = $entry->appendChild($xml->createElement($field_name)); $name->appendChild($xml->createCDATASection($field_value)); } } -
Laurence Cope revised this gist
Sep 16, 2015 . 1 changed file with 6 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ $file = fopen($filepath, "r") or die("Error opening file"); $i = 0; // Import CSV to array while(($line = fgetcsv($file)) !== FALSE) { // Get Column Names @@ -34,6 +34,11 @@ $i++; } // Process Data if need be foreach($data AS $key => $val) { // Processing here } //Creates XML string and XML document using the DOM $xml = new DomDocument('1.0', 'UTF-8'); @@ -72,5 +77,4 @@ // Save as file $xml->save('xml-import.xml'); // save as file ?> -
Laurence Cope revised this gist
Sep 16, 2015 . No changes.There are no files selected for viewing
-
Laurence Cope created this gist
Sep 16, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,76 @@ <?php $filepath = 'data.csv'; $file = fopen($filepath, "r") or die("Error opening file"); $i = 0; // IMPORT CSV LINES TO ARRAY. ANY PROCESSING HERE TOO. while(($line = fgetcsv($file)) !== FALSE) { // Get Column Names if($i == 0) { $c = 0; foreach($line as $col) { $cols[$c] = $col; $c++; } } // Values else if($i > 0) { $c = 0; foreach($line as $val) { // Only Process Non-Blank Values if($val != '') { // Add value to new array, with fieldname as key $data[$i][$cols[$c]] = $val; } $c++; } } $i++; } //Creates XML string and XML document using the DOM $xml = new DomDocument('1.0', 'UTF-8'); //add root $root = $xml->createElement('root'); $xml->appendChild($root); foreach($data AS $key => $val) { $entry = $xml->createElement('entry'); $root->appendChild($entry); foreach($val AS $field_name => $field_value) { $field_name = preg_replace("/[^A-Za-z0-9]/", '', $field_name); // preg_replace has the allowed characters if($field_name != 'GeographicalArea') { $name = $entry->appendChild($xml->createElement($field_name)); $name->appendChild($xml->createCDATASection($field_value)); } } } $xml->formatOutput = true; // set the formatOutput attribute of domDocument to true // Output to screen //header('Content-Type: text/xml'); //echo $xml->saveXML(); // Save as file $xml->save('xml-import.xml'); // save as file ?>