Not getting all the nodes data when parsing XML to CSV using PHP -


i have parse data xml file csv using php. have written code in php, gets few details , not extract node's details if has children. testing smaller part of xml, original file has large amount of data around 20,000 person nodes. below xml data --> list.xml.

<persons>     <person>         <id>326324</id>         <lastname>abc</lastname>         <firstname>xyz</firstname>         <middlename>pqr</middlename>         <preferredfirstname></preferredfirstname>         <details>             <jobtitle>engineer</jobtitle>             <dept>healthcare</dept>             <emp_position_no>pt0970</emp_position_no>             <emp_class_code>aj</emp_class_code>             <emp_class_desc>developer</emp_class_desc>             <emp_rank></emp_rank>             <ca_addrstreet></ca_addrstreet>             <ca_addrcity></ca_addrcity>             <ca_addrstate></ca_addrstate>             <ca_addrzip></ca_addrzip>             <ca_phone></ca_phone>             <employeeemail>abc@abda.com</employeeemail>         </details>         <projects>             <subj_crse>mktg 311</subj_crse>             <subj_crse>mktg 428</subj_crse>         </projects>     </person>     <person>         <id>956197</id>         <lastname>ytrg</lastname>         <firstname>wdes</firstname>         <middlename>bvcx</middlename>         <preferredfirstname></preferredfirstname>         <details>             <jobtitle>technician</jobtitle>             <dept>education</dept>             <emp_position_no>pt1010</emp_position_no>             <emp_class_code>aj</emp_class_code>             <emp_class_desc>technician</emp_class_desc>             <emp_rank></emp_rank>             <ca_addrstreet></ca_addrstreet>             <ca_addrcity></ca_addrcity>             <ca_addrstate></ca_addrstate>             <ca_addrzip></ca_addrzip>             <ca_phone></ca_phone>             <employeeemail>tred@hdseyy.com</employeeemail>         </details>         <projects>             <subj_crse>tchcs 321</subj_crse>         </projects>     </person> </persons> 

i able retrieve data till node preferredfirstname , after since details node has further children unable extract data. below php code:

   $xml = simplexml_load_file('list.xml');    $i = 1;               $values = [];          $columns = array('id', 'lastname', 'firstname',                     'middlename', 'preferredfirstname', 'details', 'projects');     $fs = fopen('odu.csv', 'w');    fputcsv($fs, $columns);          fclose($fs);     $node = $xml->xpath('//person');     foreach ($node $n) {                     $child = $xml->xpath('//person['.$i.']/*');               foreach ($child $value) {           $values[] = $value;                 }          $fs = fopen('test.csv', 'a');        fputcsv($fs, $values);              fclose($fs);           $values = [];            $i++;                } 

i need output in csv file each node details under separate colums below: enter image description here

i never use simplexml guess accomplish same thing using rather domdocument below - don't see, in case, real need xpath query - it's quite straightforward xml. you'll able make use of following somehow.

$strxml='<persons>             <person>                 <id>326324</id>                 <lastname>abc</lastname>                 <firstname>xyz</firstname>                 <middlename>pqr</middlename>                 <preferredfirstname></preferredfirstname>                 <details>                     <jobtitle>engineer</jobtitle>                     <dept>healthcare</dept>                     <emp_position_no>pt0970</emp_position_no>                     <emp_class_code>aj</emp_class_code>                     <emp_class_desc>developer</emp_class_desc>                     <emp_rank></emp_rank>                     <ca_addrstreet></ca_addrstreet>                     <ca_addrcity></ca_addrcity>                     <ca_addrstate></ca_addrstate>                     <ca_addrzip></ca_addrzip>                     <ca_phone></ca_phone>                     <employeeemail>abc@abda.com</employeeemail>                 </details>                 <projects>                     <subj_crse>mktg 311</subj_crse>                     <subj_crse>mktg 428</subj_crse>                 </projects>             </person>             <person>                 <id>956197</id>                 <lastname>ytrg</lastname>                 <firstname>wdes</firstname>                 <middlename>bvcx</middlename>                 <preferredfirstname></preferredfirstname>                 <details>                     <jobtitle>technician</jobtitle>                     <dept>education</dept>                     <emp_position_no>pt1010</emp_position_no>                     <emp_class_code>aj</emp_class_code>                     <emp_class_desc>technician</emp_class_desc>                     <emp_rank></emp_rank>                     <ca_addrstreet></ca_addrstreet>                     <ca_addrcity></ca_addrcity>                     <ca_addrstate></ca_addrstate>                     <ca_addrzip></ca_addrzip>                     <ca_phone></ca_phone>                     <employeeemail>tred@hdseyy.com</employeeemail>                 </details>                 <projects>                     <subj_crse>tchcs 321</subj_crse>                 </projects>             </person>         </persons>';  $dom=new domdocument; $dom->loadxml( $strxml ); /*     load file rather string of xml     ------------------------------------------     $dom->load( realpath( 'list.xml' ) ); */    /* file handles */ $output=__dir__.'\odu.csv'; $tmp=tempnam( sys_get_temp_dir(), 'csv' ); $handle=fopen( $tmp, 'r+' );    $col=$dom->getelementsbytagname('person');  if( $col ){     foreach( $col $person ){         if( $person->nodetype==xml_element_node && $person->haschildnodes() ){              /* arrays hold data */             $row=array();             $cols=array();              foreach( $person->childnodes $node ) {                  if( $node->nodetype==xml_element_node ){                      if( $node->haschildnodes() && $node->childnodes->length > 1 ) {                         foreach( $node->childnodes $detail ){                             if( $detail->nodetype==xml_element_node ) {                                  /* add values details */                                 $row[ $detail->tagname ]=$detail->nodevalue;                                  /* add column headers / tagnames nodes within details */                                 $cols[]=$detail->tagname;                             }                         }                     } else {                         $cols[]=$node->tagname;                         $row[ $node->tagname ]=$node->nodevalue;                     }                 }             }             fputcsv( $handle, $row );         }     } }  /* change handle final output file & truncate before writing */ $handle=fopen( $output, 'w+' );  /* add column headers */ fputcsv( $handle, $cols );  /* close file handle */ @fclose( $handle );  /* copy contents of temp file output */ file_put_contents( $output, file_get_contents( $tmp ), file_append );  /* delete temp file */ @unlink( $tmp );   $dom=$col=$person=$node=$handle=null; 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -