使用php从xml创建一个csv

时间:2014-12-16 01:24:14

标签: php xml-parsing export-to-csv

我有一个xml文件,我希望将其转换为csv文件。我的xml文件是包含产品编号和两种属性的产品列表

  1. attrtype=ProductattributeTypeTechData
  2. attrtype=ProductattributeTypeScopeSupply
  3. 我设法获得了所有TypeTechData属性,因为它应该在一个文件中一起存在,但我无法弄清楚如何执行TypeScopeSupply。这是XML文件

    <PRV proid="96801" id="96802" name="1.601-555.0  K 2.080">
    <PRODUCT_NUMBER>16015550</PRODUCT_NUMBER>
    <COLL languagespecific="" attrtype="ProductattributeTypeTechData" ProdID="96802"     name="SPANNUNG_COLL" product_number="16015550" dictionary_entry="Voltage" dict_id="1775">
    <PRAT languagespecific="0" attrtype="ProductattributeTypeTechData" datatype="N" ProdID="96802"   name="SPANNUNG_VON" product_number="16015550">
    <VALUE nr="1" unit="V" unit_id="3209" vo="" vo_id="">220</VALUE>
    </PRAT>
    </COLL>
    <PRAT languagespecific="0" attrtype="ProductattributeTypeScopeSupply" datatype="F" ProdID="96802" name="INTEGRIERTER_FEINWASSERFILTER" product_number="16015550" dictionary_entry="Integrated fine mesh water filter" dict_id="2258">
    <VALUE nr="1" unit="" unit_id="-1" vo="" vo_id="">1</VALUE>
    </PRAT>
    <PRAT languagespecific="0" attrtype="ProductattributeTypeScopeSupply" datatype="F" ProdID="96802" name="ADAPTER_GARTENSCHLAUCHANSCHLUSS_A3_4" product_number="16015550" dictionary_entry="Garden hose adapter A3/4&quot;" dict_id="2113">
    <VALUE nr="1" unit="" unit_id="-1" vo="" vo_id="">1</VALUE>
    </PRAT>
    <COLL languagespecific="" attrtype="ProductattributeTypeScopeSupply" ProdID="96802" name="QUICK_CONNECT_DUESEN_COLL" product_number="16015550" dictionary_entry="Kärcher Quick Connect standard nozzles" dict_id="44501">
    <PRAT languagespecific="" attrtype="ProductattributeTypeScopeSupply" datatype="F" ProdID="96802" name="QUCK_CONNECT_DUESEN_0" product_number="16015550"/>
    </COLL>
    </PRV>
    <PRV proid="188613" id="422136" name="1.602-103.0  K 2.090">
    <PRODUCT_NUMBER>16021030</PRODUCT_NUMBER>
    <COLL languagespecific="" attrtype="ProductattributeTypeTechData" ProdID="422136" name="SPANNUNG_COLL" product_number="16021030" dictionary_entry="Voltage" dict_id="1775">
    <PRAT languagespecific="0" attrtype="ProductattributeTypeTechData" datatype="N" ProdID="422136" name="SPANNUNG_VON" product_number="16021030">
    <VALUE nr="1" unit="V" unit_id="3209" vo="" vo_id="">220</VALUE>
    </PRAT>
    </COLL>
    <PRAT languagespecific="0" attrtype="ProductattributeTypeScopeSupply" datatype="F" ProdID="422136" name="INTEGRIERTER_FEINWASSERFILTER" product_number="16021030" dictionary_entry="Integrated fine mesh water filter" dict_id="2258">
    <VALUE nr="1" unit="" unit_id="-1" vo="" vo_id="">1</VALUE>
    </PRAT>
    <PRAT languagespecific="0" attrtype="ProductattributeTypeScopeSupply" datatype="F" ProdID="422136" name="ADAPTER_GARTENSCHLAUCHANSCHLUSS_A3_4" product_number="16021030" dictionary_entry="Garden hose adapter A3/4&quot;" dict_id="2113">
    <VALUE nr="1" unit="" unit_id="-1" vo="" vo_id="">1</VALUE>
    </PRAT>
    <PRAT languagespecific="0" attrtype="ProductattributeTypeScopeSupply" datatype="F" ProdID="422136" name="HOCHDRUCKPISTOLE" product_number="16021030" dictionary_entry="High-pressure gun" dict_id="2173">
    <VALUE nr="1" unit="" unit_id="-1" vo="" vo_id="">1</VALUE>
    </PRAT>
    <PRAT languagespecific="0" attrtype="ProductattributeTypeScopeSupply" datatype="S" ProdID="422136" name="REINIGUNGSMITTELSCHLAUCH" product_number="16021030" dictionary_entry="Cleaning agent hose" dict_id="2344">
    <VALUE nr="1" unit="" unit_id="-1" vo="" vo_id="">With filter</VALUE>
    </PRAT>
    <PRAT languagespecific="0" attrtype="ProductattributeTypeScopeSupply" datatype="F" ProdID="422136" name="EIN_FACH_STRAHLROHR" product_number="16021030" dictionary_entry="Single spray lance" dict_id="2247">
    <VALUE nr="1" unit="" unit_id="-1" vo="" vo_id="">1</VALUE>
    </PRAT>
    <COLL languagespecific="" attrtype="ProductattributeTypeScopeSupply" ProdID="422136" name="QUICK_CONNECT_DUESEN_COLL" product_number="16021030" dictionary_entry="Kärcher Quick Connect standard nozzles" dict_id="44501">
    <PRAT languagespecific="" attrtype="ProductattributeTypeScopeSupply" datatype="F" ProdID="422136" name="QUCK_CONNECT_DUESEN_0" product_number="16021030"/>
    </COLL>
    </PRV>
    

    我真正想要的是将每个产品保存在以产品ID命名的单独csv中 如果TypeScopeSupply中有任何值,并且只有dictionaryentry的标题仅用于那些属性,则仅存储TypeScopeSupply的数据。因为有些TypeScopeSupply没有任何值,或者说是空的。我不希望这些空属性得到回应。

    需要注意的其他事项是COLL-PRAT-VALUE的值可能为PRAT-VALUE<?php $productfile = "Internet.xml"; //Build Product List if (!$xml = simplexml_load_file($productfile)) { echo "Unable to load XML file"; } else { echo "<span style='white-space: nowrap'>"; $products = $xml->xpath('//PRV'); //Table Header foreach ($products as $product) { foreach ($product->COLL as $COLL) { foreach ($COLL->PRAT as $PRAT){ if($COLL['attrtype']=="ProductattributeTypeScopeSupply"){ $headerarray[] = $COLL['dictionary_entry']; } }; }; foreach ($product->PRAT as $PRAT) { if($PRAT['attrtype']=="ProductattributeTypeScopeSupply"){ $headerarray[] = $PRAT['dictionary_entry']; } }; }; echo "ID,"; $headerarray = array_values(array_unique($headerarray)); //Build Seperate header array for woocommerce display $header = implode($headerarray,"|"); $header = str_replace(",","",$header); $header = explode("|",$header); echo implode(",",$header); echo "<br>"; //Build Stats Array foreach ($products as $product) { echo($product->PRODUCT_NUMBER); echo ","; foreach ($product->COLL as $COLL) { if($COLL['attrtype']=="ProductattributeTypeScopeSupply"){ $name = $COLL['dictionary_entry']; $unit = ""; unset($values); foreach ($COLL->PRAT as $PRAT){ foreach ($PRAT->VALUE as $VALUE) { $values[] = (string)$VALUE . $VALUE['unit']; }; }; $statsarray[(string)$name] = implode(array_filter($values),"|"); } }; foreach ($product->PRAT as $PRAT) { if($PRAT['attrtype']=="ProductattributeTypeScopeSupply"){ $name = $PRAT['dictionary_entry']; $unit = ""; unset($values); foreach ($PRAT->VALUE as $VALUE) { $values[] = (string)$VALUE . $VALUE['unit']; }; $statsarray[(string)$name] = implode(array_filter($values),"|"); } }; //Compare against headers $statsarray = str_replace(",",".",$statsarray); $spreadsheetarray = array_fill(0, count($headerarray),""); foreach ($statsarray as $description=>$value) { $spreadsheetarray[array_search($description,$headerarray)]=$value; }; foreach ($spreadsheetarray as $description=> $value) { $spreadsheetarray[$description] = str_replace(',','.',$value); //add column for woocommerce display //$spreadsheetarray[$description] = ",".$value; }; echo (implode($spreadsheetarray,",")); echo "<BR>"; unset($statsarray); unset($spreadsheetarray); }; echo "</span>"; //End XML if }; ?> 。 即使我知道如何做到这一点,我将不胜感激。我故意在这里放一个更大的xml文件,以显示产品属性的多样性。不同的产品具有不同类型的属性。

    {{1}}

    这就是我设法做的事情。当我在浏览器中查看时,它会输出一个带有空值的csv。所有标题及其值(包括空值在此处打印) 我不知道如何避免空值及其标题并将其放在特定于产品的csv中。

1 个答案:

答案 0 :(得分:0)

我将代码更改为以下内容,它对我有用。

 foreach ($product->COLL as $COLL) {
    foreach ($COLL->PRAT as $PRAT){
        if($COLL['attrtype']=="ProductattributeTypeScopeSupply"){
        $headerarray[] =  $COLL['dictionary_entry'];
        }
    };
    };
    foreach ($product->PRAT as $PRAT) {
    if($PRAT['attrtype']=="ProductattributeTypeScopeSupply"){
    $headerarray[] =  $PRAT['dictionary_entry'];
    }
    };

    //echo "ID,";
    $headerarray = array_values(array_unique($headerarray));

    foreach ($product->COLL as $COLL) {
        if($COLL['attrtype']=="ProductattributeTypeScopeSupply"){
        $name = $COLL['dictionary_entry'];
        $unit = "";
        unset($values);
        foreach ($COLL->PRAT as $PRAT){
            foreach ($PRAT->VALUE as $VALUE) {
                    if(!empty($name)) {
                    $values[] = (string)$VALUE.$VALUE['unit'];
                    }
                };
            };
        $statsarray[(string)$name] = $name." ".implode(array_filter($values),"|");
        }
    };
    foreach ($product->PRAT as $PRAT) {
        if($PRAT['attrtype']=="ProductattributeTypeScopeSupply"){
        $name = $PRAT['dictionary_entry'];
        $unit = "";
        unset($values);
            foreach ($PRAT->VALUE as $VALUE) {
                    if(!empty($name)){
                    $values[] = $name." ". (string)$VALUE .$VALUE['unit'];
                    }
                };
        $statsarray[(string)$name] = implode(array_filter($values),"|");
        }
    };
    $statsarray = str_replace(",",".",$statsarray);

    foreach ($statsarray as $description=>$value) {
        $spreadsheetarray[array_search($description,$headerarray)]=$value;

    };
    foreach ($spreadsheetarray as $description=> $value) {
        $spreadsheetarray[$description] = str_replace(',','.',$value);

    };