php - CSV to XML add more header -
i've got following problem:
i have csv file, can convert xml via php script.
this way, every field goes under same row in xml, , want product_sku
field go under different row, example call sku_row
.
the csv looks this:
the xml looks this:
php file code run convert csv xml:
error_reporting(e_all | e_strict); ini_set('display_errors', true); ini_set('auto_detect_line_endings', true); function preparexmlname($preparestring) { $preparestring = str_replace(" ","",$preparestring); $preparestring = preg_replace('#\w#', '', $preparestring); $preparestring = str_replace("zspazesz","",$preparestring); $preparestring = strtolower($preparestring); return $preparestring; } $inputfilename = 'faszom.csv'; $outputfilename = 'faszom.xml'; // open csv read $inputfile = fopen($inputfilename, 'rt'); // headers of file $headers = fgetcsv($inputfile); // create new dom document pretty formatting $doc = new domdocument(); $doc->formatoutput = true; // add root node document $root = $doc->createelement('rows'); $root = $doc->appendchild($root); // loop through each row creating <row> node correct data while (($row = fgetcsv($inputfile)) !== false) { $container = $doc->createelement('row'); foreach ($headers $i => $header) { $header = str_replace(chr(32),"_",trim($header)); $header = strtolower($header); if($header==''){ $header = 'empty';} $header = preparexmlname($header); if(is_numeric($header)) { $header = "number-". $header; } //echo "here: " . $header . "<br />"; $child = $doc->createelement($header); $child = $container->appendchild($child); $value = $doc->createtextnode($row[$i]); $value = $child->appendchild($value); } $root->appendchild($container); } header("content-type: text/xml"); $strxml = $doc->savexml(); $handle = fopen($outputfilename, "w"); fwrite($handle, $strxml); fclose($handle); echo $doc->savexml();
how can in php file, let put product_sku
fields under different row, separated row in customer data's are?
xml allows group on further level. instead of flat row model:
<rows> <row> <product_sku>l162l</product_sku> <order_entity_id>31</order_entity_id> <order_customer_firstname>teszt</order_customer_firstname> <product_qty_ordered>1.0000</product_qty_ordered> </row>
you can group further on depending on context of data, example order, product , customer:
<rows> <row> <product> <sku>l162l</sku> <qty_ordered>1.0000</qty_ordered> <product> <order> <entity_id>31</entity_id> <customer> <firstname>teszt</firstname> </customer> </order> </row>
but depends entirely of want , question don't see reason on why this, or other way nor stands in way whatever want.
Comments
Post a Comment