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