Skip to content

Instantly share code, notes, and snippets.

@amityweb
Last active May 12, 2022 18:22
Show Gist options
  • Select an option

  • Save amityweb/479a0ca0e5ef39a786bc to your computer and use it in GitHub Desktop.

Select an option

Save amityweb/479a0ca0e5ef39a786bc to your computer and use it in GitHub Desktop.

Revisions

  1. Laurence Cope revised this gist Sep 16, 2015. 1 changed file with 6 additions and 31 deletions.
    37 changes: 6 additions & 31 deletions csv_to_xml.php
    Original file line number Diff line number Diff line change
    @@ -1,37 +1,12 @@
    <?php
    $filepath = 'data.csv';
    $file = fopen($filepath, "r") or die("Error opening file");
    $i = 0;

    // Import CSV to array
    while(($line = fgetcsv($file)) !== FALSE)
    // Map CSV file to array
    $rows = array_map('str_getcsv', file('data.csv'));
    $header = array_shift($rows);
    $data = array();
    foreach ($rows as $row)
    {
    // 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++;
    $data[] = array_combine($header, $row);
    }

    // Process Data if need be
  2. Laurence Cope revised this gist Sep 16, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion csv_to_xml.php
    Original file line number Diff line number Diff line change
    @@ -62,7 +62,7 @@

    }

    // Set the formatOutput attribute of dom to true
    // Set the formatOutput attribute of xml to true
    $xml->formatOutput = true;

    // Output to screen
  3. Laurence Cope revised this gist Sep 16, 2015. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions csv_to_xml.php
    Original 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
    //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 @@

    }

    $xml->formatOutput = true; // set the formatOutput attribute of domDocument to true
    // Set the formatOutput attribute of dom to true
    $xml->formatOutput = true;

    // Output to screen
    //header('Content-Type: text/xml');
  4. Laurence Cope revised this gist Sep 16, 2015. 1 changed file with 3 additions and 9 deletions.
    12 changes: 3 additions & 9 deletions csv_to_xml.php
    Original 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

    if($field_name != 'GeographicalArea')
    {

    $name = $entry->appendChild($xml->createElement($field_name));
    $name->appendChild($xml->createCDATASection($field_value));

    }
    }
    $name = $entry->appendChild($xml->createElement($field_name));
    $name->appendChild($xml->createCDATASection($field_value));
    }

    }

  5. Laurence Cope revised this gist Sep 16, 2015. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions csv_to_xml.php
    Original 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 LINES TO ARRAY. ANY PROCESSING HERE TOO.
    // 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


    ?>
  6. Laurence Cope revised this gist Sep 16, 2015. No changes.
  7. Laurence Cope created this gist Sep 16, 2015.
    76 changes: 76 additions & 0 deletions csv_to_xml.php
    Original 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


    ?>