Wednesday, 31 December 2014

PHP script converting XML to CSV with column headers returning blank on some headers and rows



my full XML is below which is named user.xml:



<?xml version="1.0" encoding="utf-8"?>
<users>
<user id='1'>
<user_name>John</user_name>
<user_lastname>Doe</user_lastname>
<previous_requisitions>
<requisition_code>X321</requisition_code>
<requisition_code>Y321</requisition_code>
<requisition_code>Z321</requisition_code>
</previous_requisitions>
<user_requisition>
<requisition_code>X123</requisition_code>
<requisition_title>Ssr Dev 1</requisition_title>
<requisition_relocation>10~20%</requisition_relocation>
</user_requisition>
</user>
<user id='2'>
<user_name>James</user_name>
<user_lastname>Smith</user_lastname>
<previous_requisitions>
<requisition_code>X222</requisition_code>
<requisition_code>Y222</requisition_code>
<requisition_code>Z222</requisition_code>
</previous_requisitions>
<user_requisition>
<requisition_code>Y123</requisition_code>
<requisition_title>Sr Dev 1</requisition_title>
<requisition_relocation>20~30%</requisition_relocation>
</user_requisition>
</user>
<user id='3'>
<user_name>Jess</user_name>
<user_lastname>Ssej</user_lastname>
<previous_requisitions>
<requisition_code>X111</requisition_code>
<requisition_code>Y111</requisition_code>
<requisition_code>Z111</requisition_code>
</previous_requisitions>
<user_requisition>
<requisition_code>Z123</requisition_code>
<requisition_title>Jr Dev 2</requisition_title>
<requisition_relocation>0~10%</requisition_relocation>
</user_requisition>
</user>
</users>


since not very good with XML and with the help of online resources and stackoverflow; was able to create this PHP script:



<?php
$file='user.xml';
if (file_exists($file)) {
$xml = simplexml_load_file($file);
$f = fopen('user.csv', 'w');
// array to hold the field names
$headers = array();
// loop through the first set of fields to get names
foreach ($xml->user->children() as $field) {
// put the field name into array
$headers[] = $field->getName();
}
// print headers to CSV
fputcsv($f, $headers, ',', '"');
foreach ($xml->user as $users) {
fputcsv($f, get_object_vars($users), ',', '"');
}
fclose($f);
}
?>


when i run the PHP script, the file is written but with some issues:



  1. not all headers are written.

  2. not all data are written

  3. first column is populated with "Array"


see below for screenshot of the result(open in excel): screenshot of result opened in excel


QUESTIONS:




  1. since it appears that the $headers = array(); is not looping properly. any fix on the PHP script to show all headers and the data from the XML properly?




  2. i only need the following child nodes to be exported to CSV, so these 6 will also be the headers:


    user_id,


    user_name,


    user_lastname,


    user_requisition_code,


    user_requisition_title,


    user_requisition_relocation,




pls direct me how i can make the script "selective" in which omitting or not to include other child nodes from the XML like:


previous_requisitions


requisition_code


thanks a lot for the help and happy new year!


sidenote: would be sticking with opensource scripting hoping no one will answer with suggestion to use a proprietary software.


No comments:

Post a Comment