Member Avatar for mehnihma

I have this code to parse XML file

I need to take elements "Name" and write them all to csv header (from every product in XML) with rest of the XML data,
but I need values to be as data in csv

For Now I can write just header, but I get only last value in csv, not all values
and i hve to sync values to names in csv, how to achive that?

This I have for now:

            <?php

            /* Set internal character encoding to UTF-8 */
            //mb_internal_encoding("UTF-8");
            ini_set('default_charset', 'utf-8');

            // Postavljamo vremenski limit za izvršavanje skripte
            set_time_limit(600);

            //delimiter
            define('DELIMITER','^');

            $delimiter = '^';

            function start_time(){
                $time = microtime();
                $time = explode(' ', $time);
                $time = $time[1] + $time[0];
                $start = $time;
                return $start;
            }

            function duration_time($start){
                $time = microtime();
                $time = explode(' ', $time);
                $time = $time[1] + $time[0];
                $finish = $time;
                $total_time = round(($finish - $start), 4);
                return $total_time;
            }

            $start_time=start_time();

            $xml_url='lista.xml';

            //$xml_url='product-hr-709-ProductList.xml';
            $xml = simplexml_load_file(utf8_encode($xml_url), 'SimpleXMLElement', LIBXML_NOCDATA);
            if(!$xml){echo "xml $xml_url not loaded.";exit;}

            // create array for writing header

             $csv_head = array("ProductCode","Vendor","ProductType","ProductCode","ProductDescription");
             // create array for writing data
            // $csv_rows = array($node->ProductCode,'Vendor','ProductType','ProductCode','ProductDescription');

            //jel ovo ispravno????
            foreach($xml as $data_h) {
            $csv_rows = array($data_h->ProductCode,$data_h->Vendor,$data_h->ProductType,$data_h->ProductCategory,$data_h->ProductDescription);
                    }

            foreach($xml as $node) {

             // echo $node->ProductCode,"<br/>\n"; // in csv header under ProductCode
             // echo $node->Vendor,"<br/>\n"; // in csv header under Vendor
             // echo $node->ProductType,"<br/>\n"; // in csv header under ProductType
             // echo $node->ProductCategory,"<br/>\n"; // in csv header under ProductCategory
             // echo $node->ProductDescription,"<br/>\n"; // in csv header under ProductDescription

             //echo $node->Images,"<br/>\n";
             $csv_rows = array($node->ProductCode,$node->Vendor,$node->ProductType,$node->ProductCategory,$node->ProductDescription);

                 // here I need to get all the Name values and put them in header of csv file but no doubles
                 // also to write Values for that products in csv under tab Name
                 // weight has to be also value in csv header and get value from features
                 // not all features are here and not all have to be for every product
                 // for this I need Box Weight Brutto (kg) as standalone for every product as tezina just value without kg
                 // values need to go under names in csv, duplicated names are removed from array sto that needs to be sorted
                    foreach($node->AttrList->children() as $field)
                    {

                        //echo $field["Name"],"<br />"; // this goes into csv header for name of features
                        //echo $field["Value"]; // this are product features

                        // save name to string
                        $feature_name=$field["Name"]; // name 
                        $feature_value=$field["Value"]; // value 

                        // this is for kg
                        // $tezina=$field["Value"]->"Pack Weight Brutto (kg)"; // value 
                        // echo $tezina;

                        //$feature_name=explode('"', $field["Name"]); // delimiter is the last?


                        //add to header array
                        array_push($csv_head, $feature_name);
                        //add to header array
                        array_push($csv_rows, $feature_value);
                        //$appended_data = array_merge($csv_rows,$feature_value); 
                    }

                    // for images
                    // images have to have seperator like comma (,) when writing to csv
                    // in csv header under Images
                    foreach($node->Images->children() as $slika)
                    {
                        //echo $field["Name"],"<br />";
                        //echo $slika,",";
                        //echo $field["Value"];

                        //add to header array
                        //array_push($csv_head, $feature_name,$slika);
                        //add to header array
                        //array_push($csv_rows, $feature_value);
                    }

                    // marketing info, just tags, plain text
                    // in csv header under MarketingInfo
                    foreach($node->MarketingInfo->children() as $marketing)
                    {
                        //echo $field["Name"],"<br />";
                        ///echo $marketing,",";
                        //echo $field["Value"];
                    }


            }

            //print_r($csv_head);
            //echo $csv_head;

                foreach($csv_head as $hed) {
                   //echo $hed . "\n";
                   //$hed = explode(',', $hed);
                   echo $hed. "\n";
                }

                foreach($csv_rows as $bub) {
                   //echo $hed . "\n";
                   //$hed = explode(',', $hed);
                   echo $bub. "\n";
                }


                        //write it to csv
                        $fp = fopen('asbis_katalog.csv', 'w');

                        // non duplicates row names in csv
                        // there should be one attribut
                        $result_csv_header = array_unique($csv_head);

                        //fwrite($fp, implode($delimiter, $csv_head)."\r\n");
                        fwrite($fp, implode($delimiter, $result_csv_header)."\r\n");
                        //fputcsv($fp,implode($delimiter, $csv_head));
                        //fclose($fp);

                        // csv data
                            fwrite($fp,implode($delimiter, $csv_rows));


            echo '<br>Page generated in '.duration_time($start_time).' seconds.';


                fclose($fp);
            ?>

this is XML file, part of it:

    <?xml version="1.0" encoding="UTF-8"?>
    <ProductCatalog>
      <Product>
        <ProductCode>AXXRJ45DB92</ProductCode>
        <Vendor>INTEL</Vendor>
        <ProductType>Cable</ProductType>
        <ProductCategory>Cables</ProductCategory>
        <ProductDescription>INTEL Serial Cable (1xRJ-45 (Male) - 1xD-Sub 9-pin (DB-9)) for Intel® Server Chassis SR1300(Coronado-W), Intel® Server Chassis SR2300(Stayton-W)</ProductDescription>
        <Image>https://www.it4profit.com/catalogimg/wic/1/AXXRJ45DB92</Image>
        <ProductCard>https://content.it4profit.com/itshop/itemcard_cs.jsp?ITEM=50409104041464518&amp;THEME=asbis&amp;LANG=hr</ProductCard>
        <AttrList>
          <element Name="Cable Functionality" Value="Serial Cable"/>
          <element Name="Platform Compability" Value="PC"/>
          <element Name="Left Connector Type" Value="RJ-45"/>
          <element Name="Left Connector Gender" Value="Male"/>
          <element Name="Left Connector Quantity" Value="1"/>
          <element Name="Right Connector Type" Value="D-Sub 9-pin (DB-9)"/>
          <element Name="Right Connector Quantity" Value="1"/>
          <element Name="Parent Products(1)" Value="Intel® Server Chassis SR1300(Coronado-W), Intel® Server Chassis SR2300(Stayton-W)"/>
          <element Name="Nominal Weight" Value="0.5875 kg"/>
          <element Name="Pack Weight Brutto (kg)" Value="0.5875 kg"/>
          <element Name="Pieces in pack" Value="1"/>
        </AttrList>
        <MarketingInfo>
          <element>Intel is the world leader in the field of computer engineering, computer innovations, development of software and hardware. Specialists Intel did not stop searching for new ways to make the next leap ahead - in the IT-technology, education, culture, production and social sphere.</element>
        </MarketingInfo>
        <Images>
          <Image>https://content.it4profit.com/pimg/s/resize/160x160x160x160/110519101431810915.jpg</Image>
          <Image>https://content.it4profit.com/pimg/s/resize/400x300x400x300/110519101431813128.jpg</Image>
          <Image>https://content.it4profit.com/pimg/s/resize/362x362x362x362/110519101431861327.jpg</Image>
          <Image>https://content.it4profit.com/pimg/s/resize/362x362x362x362/110519101431818713.jpg</Image>
        </Images>
      </Product>

I need something like this:

            ProductCode,Vendor,ProductType,ProductCategory,ProductDescription,Images,Cable Functionality.....
            AXXRJ45DB92,INTEL,Cable,Cables,INTEL Serial Cable, imgurls,Serial Cable....

Recommended Answers

All 5 Replies

Member Avatar for LastMitch

@mehnihma

I need to take elements "Name" and write them all to csv header (from every product in XML) with rest of the XML data,
but I need values to be as data in csv

You know you can combine this together:

Instead of this:

fwrite($fp, implode($delimiter, $result_csv_header)."\r\n");

fwrite($fp, implode($delimiter, $csv_rows));

Try this:

fwrite($fp, implode($delimiter, $result_csv_header, $csv_rows)."\r\n");    

You can used str_getcsv() function to get all the valve:

$fp = fopen('asbis_katalog.csv', 'w');

$fp = str_getcsv($fp, $delimiter = ',')."\r\n");  

fclose($fp);
Member Avatar for mehnihma

Hi
When I do:

fwrite($fp, implode($delimiter, $result_csv_header, $csv_rows)."\r\n");

I get:

Warning: Wrong parameter count for implode() in /probe/asbisxml.php on line 161

Also for str_getcsv
I get always:

Fatal error: Call to undefined function str_getcsv() in /probe/asbisxml.php on line 167

How to properly use that? That is just for PHP > 5.3 and I use PHP 5.2.1.7 on my web hosting

Member Avatar for LastMitch

@mehnihma

Wrong parameter count for implode()

Can you provide the code for that line? So I can see what is the issue.

Right now, base on the error from this:

fwrite($fp, implode($delimiter, $result_csv_header, $csv_rows)."\r\n");

Change the $delimiter from this:

fwrite($fp, implode($delimiter, $result_csv_header, $csv_rows)."\r\n");

to this:

fwrite($fp, implode("','", $result_csv_header, $csv_rows)."\r\n");

How to properly use that? That is just for PHP > 5.3 and I use PHP 5.2.1.7 on my web hosting

You are correct. You didn't mention it before but it's OK. Try this example:

http://www.electrictoolbox.com/php-str-getcsv-function/

Member Avatar for mehnihma

Hi Again for

fwrite($fp, implode("','", $result_csv_header, $csv_rows)."\r\n");

I get:

Warning: Wrong parameter count for implode() in /probe/asbisxml.php on line 172

Member Avatar for LastMitch

@mehnihma

So line 172 is this line?:

fwrite($fp, implode("','", $result_csv_header, $csv_rows)."\r\n"); 

Try this:

fwrite($fp, implode($delimiter = ",", $result_csv_header, $csv_rows)."\r\n");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.