0

XMLファイルを解析するためのこのコードがあります

要素「名前」を取得し、それらすべてを (XML のすべての製品から) csv ヘッダーに残りの XML データと共に書き込む必要がありますが、csv のデータとして値が必要です。

今のところ、ヘッダーだけを書き込むことができますが、csv の最後の値のみを取得し、すべての値ではなく、値を csv の名前に同期する必要があります。それを達成するにはどうすればよいですか?

これは私が今持っている:

            <?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);
            ?>

これは XML ファイルで、その一部です。

    <?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>

私はこのようなものが必要です:

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

0 に答える 0